Jump to content

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 :)

 

<?php

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

// check errors
if(mysqli_connect_errno()){

	echo mysqli_connect_error();
	exit();

}


$var = 34;

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

$query->execute();

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

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

while($query->fetch()){

	$myarray[$i][] = $name;
	$myarray[$i][]= $address;
	$myarray[$i][] = $town;
	$i++;
}
echo "<pre>";
print_r($myarray);

Link to comment
https://forums.phpfreaks.com/topic/267444-mysqli-and-bind-result/
Share on other sites

As the PHP manual states:

mysqli_stmt::fetch -- mysqli_stmt_fetch ? Fetch results from a prepared statement into the bound variables

In other words, you're not fetching an array, but using a function very similar to extract (). If you want an array, don't use mysqli::bind_result (), but mysqli::fetch_array ().

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.

 

 

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?

 

Example:

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

 

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.

 

Drongo

 

 

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().

Ah, would you look at that. What a smart individual too!

:P

 

OK, time to re-read threads before answering them, so I don't make any more mistakes like this. Forgot all about that post while searching for an answer I knew I had read somewhere. :-[

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.