Jump to content

[PHP] Why a while loop for printing SQL query results?

burnttoastnice
Go to solution Solved by Neil,

Because you want to loop while you still have data.

 

The thing returned by the mysqli functions, iirc, is a resource identifier, not actual data.

 

You use a while loop to go through it so that you pull the data each time you call your fetch (datatype), and the library internally increased the iterator. Once it's at the end of the iterator, it returns null, and the loop is therefore broken.

 

Is there a reason you need a for/each loop?

Why is a while loop used instead of an  if  or a foreach loop when fetching SQL query results? 

I'm using a SQLite database as well as PHP's SQLite3 class.

Speedtests

WiFi - 7ms, 22Mb down, 10Mb up

Ethernet - 6ms, 47.5Mb down, 9.7Mb up

 

Rigs

Spoiler

 Type            Desktop

 OS              Windows 10 Pro

 CPU             i5-4430S

 RAM             8GB CORSAIR XMS3 (2x4gb)

 Cooler          LC Power LC-CC-97 65W

 Motherboard     ASUS H81M-PLUS

 GPU             GeForce GTX 1060

 Storage         120GB Sandisk SSD (boot), 750GB Seagate 2.5" (storage), 500GB Seagate 2.5" SSHD (cache)

 

Spoiler

Type            Server

OS              Ubuntu 14.04 LTS

CPU             Core 2 Duo E6320

RAM             2GB Non-ECC

Motherboard     ASUS P5VD2-MX SE

Storage         RAID 1: 250GB WD Blue and Seagate Barracuda

Uses            Webserver, NAS, Mediaserver, Database Server

 

Quotes of Fame

On 8/27/2015 at 10:09 AM, Drixen said:

Linus is light years ahead a lot of other YouTubers, he isn't just an average YouTuber.. he's legitimately, legit.

On 10/11/2015 at 11:36 AM, Geralt said:

When something is worth doing, it's worth overdoing.

On 6/22/2016 at 10:05 AM, trag1c said:

It's completely blown out of proportion. Also if you're the least bit worried about data gathering then you should go live in a cave a 1000Km from the nearest establishment simply because every device and every entity gathers information these days. In the current era privacy is just fallacy and nothing more.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Because you want to loop while you still have data.

 

The thing returned by the mysqli functions, iirc, is a resource identifier, not actual data.

 

You use a while loop to go through it so that you pull the data each time you call your fetch (datatype), and the library internally increased the iterator. Once it's at the end of the iterator, it returns null, and the loop is therefore broken.

 

Is there a reason you need a for/each loop?

--Neil Hanlon

Operations Engineer

Link to comment
Share on other sites

Link to post
Share on other sites

Because you want to loop while you still have data.

 

The thing returned by the mysqli functions, iirc, is a resource identifier, not actual data.

 

You use a while loop to go through it so that you pull the data each time you call your fetch (datatype), and the library internally increased the iterator. Once it's at the end of the iterator, it returns null, and the loop is therefore broken.

 

Is there a reason you need a for/each loop?

 

Not really any specific reason, was just curious as to why. Thanks :D

Speedtests

WiFi - 7ms, 22Mb down, 10Mb up

Ethernet - 6ms, 47.5Mb down, 9.7Mb up

 

Rigs

Spoiler

 Type            Desktop

 OS              Windows 10 Pro

 CPU             i5-4430S

 RAM             8GB CORSAIR XMS3 (2x4gb)

 Cooler          LC Power LC-CC-97 65W

 Motherboard     ASUS H81M-PLUS

 GPU             GeForce GTX 1060

 Storage         120GB Sandisk SSD (boot), 750GB Seagate 2.5" (storage), 500GB Seagate 2.5" SSHD (cache)

 

Spoiler

Type            Server

OS              Ubuntu 14.04 LTS

CPU             Core 2 Duo E6320

RAM             2GB Non-ECC

Motherboard     ASUS P5VD2-MX SE

Storage         RAID 1: 250GB WD Blue and Seagate Barracuda

Uses            Webserver, NAS, Mediaserver, Database Server

 

Quotes of Fame

On 8/27/2015 at 10:09 AM, Drixen said:

Linus is light years ahead a lot of other YouTubers, he isn't just an average YouTuber.. he's legitimately, legit.

On 10/11/2015 at 11:36 AM, Geralt said:

When something is worth doing, it's worth overdoing.

On 6/22/2016 at 10:05 AM, trag1c said:

It's completely blown out of proportion. Also if you're the least bit worried about data gathering then you should go live in a cave a 1000Km from the nearest establishment simply because every device and every entity gathers information these days. In the current era privacy is just fallacy and nothing more.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Why is a while loop used instead of an  if  or a foreach loop when fetching SQL query results? 

I'm using a SQLite database as well as PHP's SQLite3 class.

foreach is meant for looping over arrays

 

So you will need to store the results of your SQL Query in an array before using the foreach.

Just adds an extra step, instead of just fetching the data row by row, and acting on each row.

Link to comment
Share on other sites

Link to post
Share on other sites

foreach is meant for looping over arrays

 

So you will need to store the results of your SQL Query in an array before using the foreach.

Just adds an extra step, instead of just fetching the data row by row, and acting on each row.

 

If only the result was returned in a format suitable for converting into an array <_< I'll try your suggestion by looping it over and adding each row into an array.

Speedtests

WiFi - 7ms, 22Mb down, 10Mb up

Ethernet - 6ms, 47.5Mb down, 9.7Mb up

 

Rigs

Spoiler

 Type            Desktop

 OS              Windows 10 Pro

 CPU             i5-4430S

 RAM             8GB CORSAIR XMS3 (2x4gb)

 Cooler          LC Power LC-CC-97 65W

 Motherboard     ASUS H81M-PLUS

 GPU             GeForce GTX 1060

 Storage         120GB Sandisk SSD (boot), 750GB Seagate 2.5" (storage), 500GB Seagate 2.5" SSHD (cache)

 

Spoiler

Type            Server

OS              Ubuntu 14.04 LTS

CPU             Core 2 Duo E6320

RAM             2GB Non-ECC

Motherboard     ASUS P5VD2-MX SE

Storage         RAID 1: 250GB WD Blue and Seagate Barracuda

Uses            Webserver, NAS, Mediaserver, Database Server

 

Quotes of Fame

On 8/27/2015 at 10:09 AM, Drixen said:

Linus is light years ahead a lot of other YouTubers, he isn't just an average YouTuber.. he's legitimately, legit.

On 10/11/2015 at 11:36 AM, Geralt said:

When something is worth doing, it's worth overdoing.

On 6/22/2016 at 10:05 AM, trag1c said:

It's completely blown out of proportion. Also if you're the least bit worried about data gathering then you should go live in a cave a 1000Km from the nearest establishment simply because every device and every entity gathers information these days. In the current era privacy is just fallacy and nothing more.

 

Link to comment
Share on other sites

Link to post
Share on other sites

If only the result was returned in a format suitable for converting into an array <_< I'll try your suggestion by looping it over and adding each row into an array.

take a look into the fetchall command, you don't need to add each row into an array

From the manpage, mysqli_fetch_all() fetches all result rows and returns the result set as an associative array, a numeric array, or both.

Link to comment
Share on other sites

Link to post
Share on other sites

take a look into the fetchall command, you don't need to add each row into an array

From the manpage, mysqli_fetch_all() fetches all result rows and returns the result set as an associative array, a numeric array, or both.

 

I'm not sure if that function is available for SQLite - but in future I'll be moving to a proper mySQL database so I'll keep this in mind.

Speedtests

WiFi - 7ms, 22Mb down, 10Mb up

Ethernet - 6ms, 47.5Mb down, 9.7Mb up

 

Rigs

Spoiler

 Type            Desktop

 OS              Windows 10 Pro

 CPU             i5-4430S

 RAM             8GB CORSAIR XMS3 (2x4gb)

 Cooler          LC Power LC-CC-97 65W

 Motherboard     ASUS H81M-PLUS

 GPU             GeForce GTX 1060

 Storage         120GB Sandisk SSD (boot), 750GB Seagate 2.5" (storage), 500GB Seagate 2.5" SSHD (cache)

 

Spoiler

Type            Server

OS              Ubuntu 14.04 LTS

CPU             Core 2 Duo E6320

RAM             2GB Non-ECC

Motherboard     ASUS P5VD2-MX SE

Storage         RAID 1: 250GB WD Blue and Seagate Barracuda

Uses            Webserver, NAS, Mediaserver, Database Server

 

Quotes of Fame

On 8/27/2015 at 10:09 AM, Drixen said:

Linus is light years ahead a lot of other YouTubers, he isn't just an average YouTuber.. he's legitimately, legit.

On 10/11/2015 at 11:36 AM, Geralt said:

When something is worth doing, it's worth overdoing.

On 6/22/2016 at 10:05 AM, trag1c said:

It's completely blown out of proportion. Also if you're the least bit worried about data gathering then you should go live in a cave a 1000Km from the nearest establishment simply because every device and every entity gathers information these days. In the current era privacy is just fallacy and nothing more.

 

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not sure if that function is available for SQLite - but in future I'll be moving to a proper mySQL database so I'll keep this in mind.

From the manpage: 

 

sqlite_fetch_all() returns an array of the entire result set from the result resource. It is similar to calling sqlite_query() (orsqlite_unbuffered_query()) and then sqlite_fetch_array() for each row in the result set.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×