Kyle
Kyle

Reputation: 3042

mysql_fetch_row returns value above zero despite actually being zero

I'm running the following query but $new returns 0:

$count = mysql_query("SELECT COUNT(*) FROM flagged WHERE status=0") or die(mysql_error()); 
$new = mysql_fetch_row($count);

if ($new != 0) {
            echo "<script language=\"javascript\">$.titleAlert(\"New Logs - ($new[0])\");</script>";
        }

Problem is the if-condition keeps getting met when it shouldn't when $new != 0

I even tried:

if ($new > 0) {
//update title about new logs
}

Either way, the title is still updated and I'm not sure why.

New Logs - (0)

Upvotes: 0

Views: 321

Answers (4)

Piotr Salaciak
Piotr Salaciak

Reputation: 1663

this code

$new = mysql_fetch_row($count);

makes $new becomes an array

try to compare it like this:

if ($new[0] != 0)

or even better, so You would be 100% sure it's what You need.

if (count($new) == 1 && intval($new[0]) != 0)

Upvotes: 1

JEEND0
JEEND0

Reputation: 482

mysql_fetch_row should return an array or FALSE.

Maybe adjust your conditional to:

if ( !$new ) {
    // some code here
}

Upvotes: 0

Suroot
Suroot

Reputation: 4423

Not sure what you mean. $new should be an array not a scalar value per the PHP documentation.

mysql_fetch_row documentation

Upvotes: 1

Yanick Rochon
Yanick Rochon

Reputation: 53576

just do

var_dump($new);

and you'll see why your if doesn't work

Upvotes: 2

Related Questions