Home > front end >  Why is wpdb-prepare() with passed variable not working
Why is wpdb-prepare() with passed variable not working

Time:12-07

The following using wpdb->prepare returns empty results while the code below not using wpdb->prepare returns the correct results. What is wrong?

global $wpdb;

$query = $wpdb->prepare("select subid, firstname, lastname from wpks_members where member_id = %d,".$mid);
$row= $wpdb->get_row($query);

$query = $wpdb->prepare("select t.subid, t.testid, t.test_date, t.puzzle_score,t.clock_score,t.match_score,t.oddone_score,(t.puzzle_score t.clock_score t.match_score t.oddone_score) as tot, 
if((t.puzzle_score t.clock_score t.match_score t.oddone_score) < 90, 'Yes', 'No') as refer, u.ufname, u.ulname 
from wpks_results t 
join wpks_hasi_users u on t.userid = u.userid
where t.member_id = %d,".$mid);
$rows = $wpdb->get_results($query);

//this below works

$row= $wpdb->get_row("select subid, firstname, lastname from wpks_members where member_id = ".$mid);

$query = $wpdb->prepare();
$rows = $wpdb->get_results("select t.subid, t.testid, t.test_date, t.puzzle_score,t.clock_score,t.match_score,t.oddone_score,(t.puzzle_score t.clock_score t.match_score t.oddone_score) as tot, 
if((t.puzzle_score t.clock_score t.match_score t.oddone_score) < 90, 'Yes', 'No') as refer, u.ufname, u.ulname 
from wpks_results t 
join wpks_hasi_users u on t.userid = u.userid
where t.member_id = ".$mid);

CodePudding user response:

It should be:

$query = $wpdb->prepare("select subid, firstname, lastname from wpks_members where member_id = %d", $mid);
  • Related