What is the correct way to iterate over a MySQL table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

What is the correct way to iterate over a MySQL table

Post by Kadanis »

Hi

I've recently started using Zend as an IDE for PHP coding and it throws a warning on the way I code my table loops. I wondered if this the way I'm doing it is wrong/incorrect and if so whats the right way ;)

The error that is shown is using assigns in a loop

Thanks
D

Code: Select all

//connect to db
$con = mysql_connect($host,$user,$pass);

//get table names
$tables = mysql_query("SHOW TABLES FROM db_mine",$con);

//iterate over table names
while($row = mysql_fetch_array($tables)){
	
	//get tablename from the array
	$tableName = $row[0];
	
//display table name on new line
        echo "$tableName <br />";
}



//release recordset
mysql_free_result($tables);

//close the connection, block warnings
@mysql_close($con);
The code above is only to show how I would loop over a table resource and is not any particular function I'm trying to acheive ;)
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Yes! That is the right way.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: What is the correct way to iterate over a MySQL table

Post by volka »

Kadanis wrote:and it throws a warning on the way I code my table loops.
Is it a secret warning message?

try

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);

$con = mysql_connect($host,$user,$pass) or die(mysql_error());

$tables = mysql_query('SHOW TABLES FROM db_mine', $con) or die(mysql_error());
while( $row=mysql_fetch_array($tables, MYSQL_NUM) ) {
	echo $row[0], "<br />\n";
}
?>
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Post by Kadanis »

secret warning?? sorry not sure i get what you mean.

as for your code, i tried that and its still the same, but this is a warning in Zend not in the actual running of the php.

I just like clean code and wondered if this was anything to worry about. You know, like if I was using some hack way of doing it instead of a proper way.

the error is "Assignment in condition while( $row=mysql_fetch_array($tables, MYSQL_NUM)"

but if it works and you think this is an ok way to do it then, maybe its just Zend being sensitive ;)

thanks for the replies
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Kadanis wrote:secret warning?? sorry not sure i get what you mean.
He means, if you get a warning, it may help if you actually post it. It will help us help you. Unless of course it is a secret :wink: :lol:

But you are right, this is just a Zend thing
Zend Forums wrote:Such statements, which are perfectly legal, are indeed sometimes a typo error, when the programmer forgets to put the double "=" equal sign and puts only one.
See the Code Analyzer full description - it says itself that this is sometimes indicating a bug, not always, like in your case.
To avoid the message, you could do this

Code: Select all

while( ( $row=mysql_fetch_array($tables, MYSQL_NUM) ) ) {
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Everyone has missed that the actual warning is because the value of $tableName will be overwritten with every iteration :P
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Jenk wrote:Everyone has missed that the actual warning is because the value of $tableName will be overwritten with every iteration :P
:?:
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

$tableName = $row[0];
The warning is generated by the Zend PHP IDE - it is not an E_WARNING message.

The message is along the lines of "Use of Assignment operator in loop" ergo, lopping is pointless as it will overwrite the value with every iteration.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Jenk wrote:Everyone has missed that the actual warning is because the value of $tableName will be overwritten with every iteration :P
Jenk wrote:The message is along the lines of "Use of Assignment operator in loop" ergo, lopping is pointless as it will overwrite the value with every iteration.
I dont think it is. Overwriting the $table var looks to me like the expected behavior.

The Zend warning was due to the fact the while loop statement could have been wrong.

You know, when you accidentally put a single equal '=' instead of two '=='

Code: Select all

if($somevar = 1)
{
    // always equates to TRUE
}
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Post by Kadanis »

JayBird wrote:
Kadanis wrote:secret warning?? sorry not sure i get what you mean.
He means, if you get a warning, it may help if you actually post it. It will help us help you. Unless of course it is a secret :wink: :lol:
thought I did, although obviously re-reading my OP i didn't lol. sorry about that :S ;)
Jaybird wrote: But you are right, this is just a Zend thing
Zend Forums wrote:Such statements, which are perfectly legal, are indeed sometimes a typo error, when the programmer forgets to put the double "=" equal sign and puts only one.
See the Code Analyzer full description - it says itself that this is sometimes indicating a bug, not always, like in your case.
To avoid the message, you could do this

Code: Select all

while( ( $row=mysql_fetch_array($tables, MYSQL_NUM) ) ) {
Well I guess that's the final answer I was looking for. My code is ok, its just Zend being over cautious. :)

Thanks for the replies all :)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Other languages (e.g. C#) force the expression in a condition to be of the type boolean (no implicit typecast).
e.g.

Code: Select all

$a = 0;

// valid in php, c, ... not valid in c#
if (!$a) { whatever(); }

// valid
if ( 0==$a ) { whatever(); }

// valid (but not possible in C#, strong-typed)
while( false===($row=mysql_fetch_array($tables, MYSQL_NUM)) ) {
Jenk wrote:The message is along the lines of "Use of Assignment operator in loop" ergo, lopping is pointless as it will overwrite the value with every iteration.
no.
Kadanis wrote:the error is "Assignment in condition while( $row=mysql_fetch_array($tables, MYSQL_NUM)"
Post Reply