sql - PreparedStatement select with php not working as expected -


i have query:

select id, result, ip_address, added_date results course_id = (  select id courses course =  'informatica' , macro_course_id = (  select id macro_courses macro_course =  'scienze-matematiche-fisiche-e-naturali' , organization_id = (  select id organizations organization =  'universita-degli-studi-di-torino' , city_id = (  select id cities city =  'torino' , region_id = (  select id regions region =  'piemonte' ))))) order id desc 

and i'm using code preparedstatement

public function getresults($region, $city, $organization, $macro_course, $course) { //works     //added_date=datetime : yyyy-mm-dd hh:mm:ss     echo "select id, result, ip_address, added_date     results     course_id = (      select id     courses     course =  '$course'     , macro_course_id = (      select id     macro_courses     macro_course =  '$macro_course'     , organization_id = (      select id     organizations     organization =  '$organization'     , city_id = (      select id     cities     city =  '$city'     , region_id = (      select id     regions     region =  '$region' ))))) order id desc"; //just me know query being executed        if ($stmt = $this->mysqli->prepare(("     select id, result, ip_address, added_date     results     course_id = (      select id     courses     course =  ?     , macro_course_id = (      select id     macro_courses     macro_course =  ?     , organization_id = (      select id     organizations     organization =  ?     , city_id = (      select id     cities     city =  ?     , region_id = (      select id     regions     region =  ? ))))) order id desc    "))) {         $return = array();         $stmt->bind_param('sssss', $course, $macro_course, $organization, $city, $region);         $stmt->execute();         if ($stmt->fetch()) {             $i = 0;             while ($row = $stmt->fetch()) {                 print_r($row);//this never reached                 continue;                 $s = new result($row['result'], $row['added_date'], $row['id']);                 $return[$i] = $s;                 $i+=1;             }         }     }     return $return; } 

the problem function returns 0 rows , 0 errors (checked $this->mysqli->error), seems $row = $stmt->fetch() false.

however, if copy , execute on phpmyadmin output @ function top, see

showing lines 0 - 0 ( 1 total, query time 0.0003 sec) 

so query returns line not catched php. missing? how can fix this?

because used $stmt-fetch() 2 times here

if ($stmt->fetch()) {         $i = 0;         while ($row = $stmt->fetch()) { 

remove if ($stmt->fetch()) condition, work expected.

edit

from docs

note columns must bound application before calling $stmt->fetch().

you have bind result before calling $stmt->fetch() this

/* bind result variables */ $stmt->bind_result($name, $code); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -