0

First off, yes, I know mysql_num_rows() is deprecated.

Now, continuing on to the actual question, I'm making a page where users can post things and other users can like them. But when the post displays, and the user viewing them didn't/hasn't liked the post, the mysql_num_rows() doesn't work.

Here's my code:

<?
$plikes=mysql_query("SELECT * FROM postLikes WHERE username='$myusername' AND postId='$postId'");
$plikesRows=mysql_num_rows($plikes)or die("error: ".mysql_error());
if ($plikesRows>=1) {
//stuff
}
else {
//other stuff
}
?>

the variables $myusername and $postId are set correctly, and I tested the query and it works without any errors. But when I go onto the page, all it displays is a "error:", without any error.

John Conde
  • 217,595
  • 99
  • 455
  • 496
kzhao14
  • 2,470
  • 14
  • 21
  • if you know it's deprecated, why are you continuing to use? If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jul 08 '15 at 16:11
  • It's because I'm working with an older system. – kzhao14 Jul 08 '15 at 16:26

1 Answers1

1

That line of code is not doing what you think it is. mysql_num_rows($plikes) becomes 0 which is type juggled to false so the second half of the OR statement is triggered. This happens because by using OR you turned the results of line of code into a boolean evaluation.

$plikes=mysql_query("SELECT * FROM postLikes WHERE username='$myusername' AND postId='$postId'");
if (!mysql_error()) {
    $plikesRows=mysql_num_rows($plikes);
    if ($plikesRows>=1) {
    //stuff
    }
    else {
    //other stuff
    }
}
else {
    die("error: ".mysql_error());
}
John Conde
  • 217,595
  • 99
  • 455
  • 496