Jump to content

Trouble converting mysql statement to a mysqli prepared statement


Recommended Posts

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);
}
?>

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?

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);
}
?>

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);
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.