Jump to content

MYSQLI and bind result


Recommended Posts

Hi Guys!


Quick question on mysqli and bind_result (been using pdo a lot and mysqli seems a wee bit different which is throwing me).


When you bind the results of a query, what is the best way to get a multidimensional array from the result?


I have played about and got the below to work - but i just know there has to be a better way than that!


I woudl appreciate some enlightenment if you please :)



$db = new mysqli("localhost", "root", "", "testingcsv");

// check errors

	echo mysqli_connect_error();


$var = 34;

$query = $db->prepare("SELECT name, address, town FROM datastuff WHERE id=?");
$query->bind_param('i', $var);


$query->bind_result($name, $address, $town);

$myarray = array(); // to store results
$i = 0; 


	$myarray[$i][] = $name;
	$myarray[$i][]= $address;
	$myarray[$i][] = $town;
echo "<pre>";

Link to comment
Share on other sites

Since you are using a prepared statement, in general, you would need to do it the way you came up with. I would form an array with keys/values and do this all in one simple statement - $myarray[] = array('name'=>$name,'address'=>$address,'town'=>$town);.


If you have php5.3+ and are using the mysqlnd driver, you can convert the result from a prepared statement into a mysqli result set using - http://php.net/manual/en/mysqli-stmt.get-result.php This will allow you to use traditional mysqli fetch statements to iterate over the result set.



Link to comment
Share on other sites

Hi Guys


Thanks for your replies to this post. Just have a couple of questions (humour me as i am still getting to grips with prepared mysqli statements).


1) Generally speaking in what scenario would you want to use bind_result? I assumed from the examples that it was simply what was done - from following examples.


2) Christian suggested above that instead of using bind result I should use mysqli_stmt::fetch. But in the context of a prepared statement, like the example below, how would I use that method?



        $var = 34;	
$query = $db->prepare("SELECT name, address, town FROM datastuff WHERE id=?");
$query->bind_param('i', $var);


Where would I go from here to use mysqli_stmt_fetch? My confusion is really around the fact that with a prepared statement you're not using somehting like:


$result = $mysqli->query($query);



Sorry if these questions seem a little vacuous...just trying to get to grips with it.





Link to comment
Share on other sites

bind_result and  mysqli_stmt::fetch are used with prepared statements.


He suggested using mysqli::fetch_array (no stmt in it), which isn't used with prepared statements. It's used with non-prepared statements that are executed via $mysqli->query().

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

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

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.