jason310771 Posted February 1, 2013 Share Posted February 1, 2013 I have been for a few days not converting one of my sites so it uses mysqli from mysql. I have managed with the little help of google that i did find change most of the mysql to mysqli but now got a problem with mysql_results. this is my original code... $res = db_query($mysqli, $sql); if(@$res->num_rows && @mysql_result($res, 0 ,'verified') == "Yes" && @mysql_result($res, 0 ,'suspended') == "No") { if(@mysql_result($res, 0 ,'changeofpasswordcode') != "") { $randomcode = @mysql_result($res, 0 ,'changeofpasswordcode'); } else { $randomcode = createPasswordResetCode(); } // do something } but when i changed it to what i thought it should be i am getting errors... $res = db_query($mysqli, $sql); if($res->num_rows && $res->fetch_object()->verified == "Yes" && $res->fetch_object()->suspended == "No") { if($res->fetch_object()->changeofpasswordcode != "") { $randomcode = $res->fetch_object()->changeofpasswordcode; } else { $randomcode = createPasswordResetCode(); } // do something } the errors are... Trying to get property of non-object on this line... $res->fetch_object()->suspended == "No") { can anyone see what i might be doing wrong to getting the data from the single row from the database ? Quote Link to comment Share on other sites More sharing options...
requinix Posted February 1, 2013 Share Posted February 1, 2013 Every time you call fetch_object() you're fetching a new row from the results. Fetch one row/object separately and reference that. $res = db_query($mysqli, $sql); $obj = $res->fetch_object(); if($res->num_rows && $obj->verified == "Yes" && $obj->suspended == "No") { if($obj->changeofpasswordcode != "") { $randomcode = $obj->changeofpasswordcode; } else { $randomcode = createPasswordResetCode(); } // do something } Quote Link to comment Share on other sites More sharing options...
jason310771 Posted February 1, 2013 Author Share Posted February 1, 2013 I see, but what if there are no results in $res, would... $obj = $res->fetch_object(); kick up an error ? Every time you call fetch_object() you're fetching a new row from the results. Fetch one row/object separately and reference that. $res = db_query($mysqli, $sql); $obj = $res->fetch_object(); if($res->num_rows && $obj->verified == "Yes" && $obj->suspended == "No") { if($obj->changeofpasswordcode != "") { $randomcode = $obj->changeofpasswordcode; } else { $randomcode = createPasswordResetCode(); } // do something } Quote Link to comment Share on other sites More sharing options...
requinix Posted February 1, 2013 Share Posted February 1, 2013 Take a look. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 1, 2013 Share Posted February 1, 2013 Make up your mind whether you want to use mysqli procedural functions or object methods. Don't mix'n'match. personally I prefer objects eg $db = new mysqlli ($host, $usr, $pwd, $dbname); $res = $db->query("SELECT foo FROM bar"); while ($res->fetch_assoc()) { // process row } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.