darklighterz Posted January 14, 2012 Share Posted January 14, 2012 Im in the proces of learning and converting some mysql statements into mysqli prepared statements However i am stuck with this one anybody able to help? The error i get is Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in........ My understanding is that this error is due to my while loop failing due to it not obtaining some needed criterea....however i have no idea why as it works perfectly if i dont use a prepared statement, and i just cant figure out whats going on. Any help would be greatly appreciated. The code is... <?php $dbc = mysqli_connect("localhost", "user", "pass", "databasename") or die("Problem connecting: ".mysqli_error()); $Output = ""; if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { $Output = "<h2 align='center'>No data to render page</h2>"; } else { $i = 0; foreach ($_SESSION["cart_array"] as $each_item){ $unique_id = $each_item["unique_id"]; $query = "SELECT firstname, lastname, location FROM people WHERE id=?"; $stmt = mysqli_prepare($dbc, $query) or die(mysqli_error($dbc)); mysqli_bind_param($stmt, "i", $unique_id); mysqli_stmt_execute($stmt); /* do i need to store the result??? is there any down side in doing so */ $result = mysqli_stmt_store_result($stmt); /* ive tried using $row = mysqli_fetch_array($result) below aswel to no avail */ while($row = mysqli_fetch_array($stmt)) { $fname = $row["firstname"]; $lname = $row["lastname"]; $uname = $row["location"]; } $Output .= 'Name: ' .$fname. ' ' .$lname. '<br />' .$uname. '<br /><br />'; } mysqli_free_result($result); mysqli_stmt_close($stmt); mysqli_close($dbc); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/255014-trouble-converting-mysql-statement-to-a-mysqli-prepared-statement/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 14, 2012 Share Posted January 14, 2012 That's not how mysqli_stmt_store_result is used. I'll get back with an answer shortly... Edit: When you use mysqli_stmt_store_result, you will still use mysqli_stmt_fetch($stmt) to fetch each row. Perhaps you were trying to use mysqli_stmt_get_result? Which would allow you to use mysqli_fetch_xxxxx statements to fetch each row? Quote Link to comment https://forums.phpfreaks.com/topic/255014-trouble-converting-mysql-statement-to-a-mysqli-prepared-statement/#findComment-1307606 Share on other sites More sharing options...
darklighterz Posted January 14, 2012 Author Share Posted January 14, 2012 That's not how mysqli_stmt_store_result is used. I'll get back with an answer shortly... Edit: When you use mysqli_stmt_store_result, you will still use mysqli_stmt_fetch($stmt) to fetch each row. Perhaps you were trying to use mysqli_stmt_get_result? Which would allow you to use mysqli_fetch_xxxxx statements to fetch each row? Thank you, I had been stumped for two days. your reply led me down a new avenue and i ended up with this.... Is there any other way for me to not have an empty while loop??? the $Output below is for testing purposes i just want the variables $fname, $lname and $uname to have the appropriate db information for each row id, so i can use that info elsewhere. If theres a better way of doing this please let me know. <?php $query = "SELECT firstname, lastname, location FROM people WHERE id=?"; $stmt = mysqli_prepare($dbc, $query) or die(mysqli_error($dbc)); mysqli_bind_param($stmt, "i", $item_id); /* execute query */ mysqli_stmt_execute($stmt); mysqli_stmt_bind_result($stmt, $fname, $lname, $uname); while(mysqli_stmt_fetch($stmt)) { } $Output .= 'Name: ' .$fname. ' ' .$lname. '<br />' .$uname. '<br /><br />'; } mysqli_free_result($result); mysqli_stmt_close($stmt); mysqli_close($dbc); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/255014-trouble-converting-mysql-statement-to-a-mysqli-prepared-statement/#findComment-1307625 Share on other sites More sharing options...
PFMaBiSmAd Posted January 14, 2012 Share Posted January 14, 2012 There's no stored result, so - mysqli_free_result($result); isn't needed (it would be mysqli_free_result($stmt); if it was being used.) Quote Link to comment https://forums.phpfreaks.com/topic/255014-trouble-converting-mysql-statement-to-a-mysqli-prepared-statement/#findComment-1307627 Share on other sites More sharing options...
darklighterz Posted January 14, 2012 Author Share Posted January 14, 2012 There's no stored result, so - mysqli_free_result($result); isn't needed (it would be mysqli_free_result($stmt); if it was being used.) Oh yea, forgot about that in my excitement.lol. Im going to be using it anyway as i realised a better way rather than needlessly using a while loop (I think this is what i was originally going for before troubleshooting and frustration mutated my code.lol). Again thank you very much for your help! You made me re-read my code with a fresh pair of eyes so to speak and as always the solution seemed so obvious. <?php $dbc = mysqli_connect("localhost", "user", "pass", "databasename") or die("Problem connecting: ".mysqli_error()); $Output = ""; if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { $Output = "<h2 align='center'>No data to render page</h2>"; } else { $i = 0; foreach ($_SESSION["cart_array"] as $each_item){ $unique_id = $each_item["unique_id"]; $query = "SELECT firstname, lastname, location FROM people WHERE id=?"; $stmt = mysqli_prepare($dbc, $query) or die(mysqli_error($dbc)); mysqli_bind_param($stmt, "i", $unique_id); mysqli_stmt_execute($stmt); mysqli_stmt_bind_result($stmt, $fname, $lname, $uname); mysqli_stmt_store_result($stmt); mysqli_fetch($stmt); $Output .= 'Name: ' .$fname. ' ' .$lname. '<br />' .$uname. '<br /><br />'; } mysqli_free_result($stmt); mysqli_stmt_close($stmt); mysqli_close($dbc); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/255014-trouble-converting-mysql-statement-to-a-mysqli-prepared-statement/#findComment-1307636 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.