Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: Assignment in condition

One thing that has always bugged me is that when checking my PHP scripts for problems, I get the warning "bool-assign : Assignment in condition" and I get them a lot.

For example:

$guests = array();
$sql = "SELECT * FROM `guestlist`";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
    $guests[] = $row['name'];

Is there a different way to get multiple or all rows into an object or array? Or is there nothing wrong with this method?

like image 862
Moak Avatar asked Dec 06 '22 06:12

Moak


1 Answers

Try doing this instead:

$guests = array();
$sql = "SELECT * FROM `guestlist`";
$result = mysql_query($sql);
while(($row = mysql_fetch_assoc($result)) !== false)
    $guests[] = $row['name'];

I believe PHP is warning because of the $row = mysql_fetch_assoc($result) not returning a Boolean.

like image 77
Jeremy Stanley Avatar answered Dec 08 '22 13:12

Jeremy Stanley