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!
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
//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
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
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.
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 '=='
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.
$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)"