4

I am using zend studio for coding in php.

In zend studio the line of while... in following code shows assignment in condition warning but it work fine and show me tables instantly.

$oDB = new db;
print '<strong>Twitter Database Tables</strong><br />';
$result = $oDB->select('SHOW TABLES');
while ($row = mysqli_fetch_row($result)) {
    print $row[0] . '<br />';
} 

But when I solve this warning with the following code it don't show warning in zend studio but it show me tables after long time about 20 to 30 seconds and long white space under results. Why?

$oDB = new db;
print '<strong>Twitter Database Tables</strong><br />';
$result = $oDB->select('SHOW TABLES');
while (( $row = mysqli_fetch_row($result)) !==FALSE) {
    print $row[0] . '<br />';
} 
Sohail Ahmad
  • 256
  • 2
  • 13
  • Are you seeing that warning in your logs? If you're not, I wouldn't worry about it. You can freely do that assignment in the while with no repercussions, as far as I am concerned. – Sebastian Frohm Dec 26 '12 at 17:16
  • It could have something to do with your DB structure. Have you tried a simple query that should be fast to see if it still hangs? Something like: 'SELECT * FROM table LIMIT 1' – Alex Dec 26 '12 at 17:16
  • yes it doesn't show any warning in logs. but why it slow down script? – Sohail Ahmad Dec 26 '12 at 17:18
  • @Alex only when I add `!==FALSE` then it slow down – Sohail Ahmad Dec 26 '12 at 17:19

1 Answers1

5

According to the manual:

mysqli_fetch_row() returns an array of strings that corresponds to the fetched row or NULL if there are no more rows in result set.

You're checking for a BOOL response, so the script continues on. Use either != FALSE or !== NULL.

The code would either be:

while (($row = mysqli_fetch_row($result)) != FALSE) {

OR

while (($row = mysqli_fetch_row($result)) !== NULL) {
Salman A
  • 262,204
  • 82
  • 430
  • 521
aynber
  • 22,380
  • 8
  • 50
  • 63
  • It'll work, and isn't truly wrong. http://php.net/manual/en/types.comparisons.php has the comparisons. With ==, NULL will equate to FALSE, but never with === . – aynber Dec 26 '12 at 17:21
  • That's right, not _truly_ wrong. It will confuse others into thinking that mysqli_fetch_row returns array or FALSE. – Salman A Dec 26 '12 at 17:23
  • problem was with `!==FALSE` and use of `!=FALSE or !==Null` solve the problem – Sohail Ahmad Dec 26 '12 at 17:26