Jump to content

mysqli fetch_all() and fetch_array() errors


nodirtyrockstar

Recommended Posts

I'm using PHP 5.3.13.

 

I am just using a very simple query to grab one column of results from a table. I would like to fetch them and then iteratively add them to a dropdown menu. I am trying to understand these methods/functions and seem to be missing something. My research on Google didn't give me any indication of what I'm doing wrong.

 

I start with one of the simplest queries possible, which I know will return a data set as I have tested it in mysqladmin. The problem arises when I try to call the method on the mysqli_result object.

 

 

$query = "SELECT `artist` FROM `bands`;";
$result = $mysqli->query($query);

$bandArr = $result->fetch_all();

 

The error I'm getting from the above code is: Fatal error: "Call to undefined method mysqli_result::fetch_all()..." I researched this error and read somewhere that you need mysqlnd. Is that true? Do I need to look into my PHP configuration to get this to work? Is it worth it for this task?

 

Then I tried fetch_array...

 

 

$query = "SELECT `artist` FROM `bands`;";
$result = $mysqli->query($query);

$bandArr = $result->fetch_array(MYSQLI_NUM);
printf("%s\n%s", $bandArr[0], $bandArr[1]);

 

And the above code for some reason returns an array with only one item, and this error: "Notice: Undefined offset: 1 in..."

 

What am I missing here? Again, all I want is a small result set from one column which can be iterated and each value added to a drop down menu.

 

Thoughts? Suggestions?

 

Thanks in advance for any help you can offer...

Link to comment
https://forums.phpfreaks.com/topic/272871-mysqli-fetch_all-and-fetch_array-errors/
Share on other sites

Try this:

 

$bandArr = $result->fetch_array(MYSQLI_NUM);

echo '<pre>' . print_r($bandArr,true) . '</pre>';

 

What do you get?

 

EDIT: Actually, the undefined offset error is because your query is only returning a single column.

fetch_array() will return only one row. You need to use it in a loop to process all of the rows:

 

$query = "SELECT `artist` FROM `bands`;";
$result = $mysqli->query($query);

while ($bandArr = $result->fetch_array(MYSQLI_NUM)) {
 printf("%s\n", $bandArr[0]);
}

 

Of course, since you are only selecting one column, there will only be one element in the $bandArr array (it will be element zero).

The error I'm getting from the above code is: Fatal error: "Call to undefined method mysqli_result::fetch_all()..." I researched this error and read somewhere that you need mysqlnd. Is that true? Do I need to look into my PHP configuration to get this to work? Is it worth it for this task?

 

Can anyone answer this for me: what is the issue with fetch_all()?

 

mysqli_fetch_all() is only available with the native driver. I've never used it (fetch_all(), that is). I've never really wanted to use it. Even for populating dropdown selections, I just use a loop. I don't think you will see enough benefit to justify the time and effort to reconfigure PHP.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.