Page 1 of 1
What is the correct way to iterate over a MySQL table
Posted: Wed Nov 15, 2006 3:59 am
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

Posted: Wed Nov 15, 2006 4:20 am
by dibyendrah
Yes! That is the right way.
Re: What is the correct way to iterate over a MySQL table
Posted: Wed Nov 15, 2006 4:33 am
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";
}
?>
Posted: Wed Nov 15, 2006 6:12 am
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
Posted: Wed Nov 15, 2006 6:24 am
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
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) ) ) {
Posted: Wed Nov 15, 2006 6:49 am
by Jenk
Everyone has missed that the actual warning is because the value of $tableName will be overwritten with every iteration

Posted: Wed Nov 15, 2006 6:51 am
by volka
Jenk wrote:Everyone has missed that the actual warning is because the value of $tableName will be overwritten with every iteration


Posted: Wed Nov 15, 2006 7:04 am
by Jenk
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.
Posted: Wed Nov 15, 2006 8:06 am
by JayBird
Jenk wrote:Everyone has missed that the actual warning is because the value of $tableName will be overwritten with every iteration

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
}
Posted: Wed Nov 15, 2006 8:49 am
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
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

Posted: Wed Nov 15, 2006 11:04 am
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)"