Working with arrays and mysqli_fetch_all

Hi,

New here and to PHP, but have previously used ASP.

I’m trying to fetch the results of a DB query into an array and then loop through it. Here’s my code:

[php]<?php
$rsCategories = mysqli_query($connBike,“SELECT CategoryID, CategoryName FROM tbl_Categories”);

// Fetch all
$arrCategories = mysqli_fetch_all($rsCategories,MYSQLI_ASSOC);

// Free result set
mysqli_free_result($rsCategories);
mysqli_close($connBike);

list($intID, $strCategory) = each($arrCategories);
while (list($intID,$strCategory) = each($arrCategories)) {
echo("$intID - $strCategory
\n");
}

?> [/php]

However, I’m obviously missing something as this the result:

Notice: Array to string conversion in C:\xampp\htdocs\PHPsites\Bikes\zzSiteFiles\AdInsert.php on line 15
1 - Array

Notice: Array to string conversion in C:\xampp\htdocs\PHPsites\Bikes\zzSiteFiles\AdInsert.php on line 15
2 - Array

Notice: Array to string conversion in C:\xampp\htdocs\PHPsites\Bikes\zzSiteFiles\AdInsert.php on line 15
3 - Array

etc.

Whereas I want the second variable $strCategory to display the DB value, which would look something like this:

1 - Frames
2 - Forks
3 - Drivetrain
etc.

Any ideas?

I’m not really sure where your array to string error is coming from. What happens if you use a foreach instead?

[php]

<?php $rsCategories = mysqli_query($connBike,"SELECT CategoryID, CategoryName FROM tbl_Categories"); // Fetch all $arrCategories = mysqli_fetch_all($rsCategories,MYSQLI_ASSOC); // Free result set mysqli_free_result($rsCategories); mysqli_close($connBike); foreach($arrCategories as $key => $value) { echo $value['CategoryID'] . " - " . $value['CategoryName'] . "
\n"; } ?>

[/php]

On that note, I wonder if your list above the while is causing the $arrCategories to convert to a string? It shouldn’t be there regardless…

Comment out this line above your while loop

[php]list($intID, $strCategory) = each($arrCategories);[/php]

Thank you so much, that worked perfectly. 8)

On that note, I wonder if your list above the while is causing the $arrCategories to convert to a string? It shouldn’t be there regardless…

No idea. :slight_smile:

I’m learning PHP having been using ASP/VBScript for quite a few years, so while I’m familiar with a lot of programming concepts, I think it’s going to take me a little while to get my head around the syntax and some of the other differences in PHP.

The www.php.net documentation was a bit sketchy about mysqli_fetch_all, it shows how to fetch the results into an array, but not how to work with that array once you’d created it. Do you know any other useful PHP reference sites?

In any case, thanks again.

The manual should be all you need but I’m sure you can find some mysqli tutorials online. mysqli_fetch_all isn’t necessarily the best function to use if all you are doing is echoing the results. The code could be simplified, for example:

[php]

<?php if ($rsCategories = $connBike->query("SELECT CategoryID, CategoryName FROM tbl_Categories")) { while($row = $rsCategories->fetch_assoc()) { echo $row['CategoryID'] . " - " . $row['CategoryName'] . "
\n"; } } $connBike->close(); ?>

[/php]

I also recommend using the object oriented style but that’s just personal preference.

e.g. $conn->query("") versus mysqli_query($conn, “”)

I found lots of examples with loops, while, for each etc., however, in ASP looping through recordsets is considered bad practice as you’re holding open the DB connection while looping through the records, so I was trying to emulate using ADODB GetRows() to load a result set into an array, which allows you to then close the DB connection before you loop through the array.

In addition, you can fetch all the data, load it into arrays or variables and close all the connections before any HTML, then within the HTML use the arrays and variables to display the output.

In most of the PHP examples I’ve seen, people happily use loops within HTML. Is this because PHP is not effected by this, or simply that people don’t bother?

There’s no single solution. It entirely depends on what you are coding. I would say consider two things…

  • If there is a very slow process within your loop, it could cause the mysql connection to close, which would cause your loop to fail. In this case you would want to build an array and close the connection.

  • Consider array size. The larger your array the more memory you are using. So loading thousands of rows into an array could cause issues with memory_limit. In this case it would be better to loop each row.

OK, in this instance I’m using the arrays to populate drop-down lists, so tens of records, not thousands, but I’ll bear that in mind.

With ADODB Getrows(), you could specify the number of rows and the first row position to return, for example Getrows(50,100), this would return 50 rows, starting at row 100, so you can use this to page through a recordset, whilst limiting the amount of data you retrieve to that required for each page. I was hoping to do something similar with PHP. ;D

Yes you can use LIMIT in mysql to create pagination. I just helped someone recently setup pagination

http://www.phphelp.com/forum/index.php?topic=19954.0

Thanks for that, I’ll take a look at that when I get to that bit of “My first PHP site”. :slight_smile:

I have to say things seem to have improved since the last time, about 7 years ago, I tried PHP/MySQL/Apache, when after trying in vain for two days to get Apache and MySQL working on my local Windows PC, I gave up in frustration and never even got to PHP.

I use WampServer for this. Super easy just install, run, and you have apache/mysql/php running :slight_smile:

There is also XAMPP but I like WAMP better :wink:

I use WampServer for this. Super easy just install, run, and you have apache/mysql/php running :)

There is also XAMPP but I like WAMP better :wink:

Yes, I used XAMPP, which also installed phpMyAdmin, there was none of this stuff years ago when I last tried it. Being used to Windows, IIS, SQL Server, where you just run an installation disc or .exe select a few options and you’re up and running, trawling through reams of documentation just to get something installed was too much like hard work, after all, I want to spend my time building web sites, not spend it learning how to get the framework set-up.

Sponsor our Newsletter | Privacy Policy | Terms of Service