2
SELECT * FROM dislikes WHERE pid = $post AND uid = $userid AND like = 1;

This query doesn't work (and I don't know why). Once I execute this query using mysqli and try to find num rows, I get

Notice: Trying to get property of non-object in C:\wamp\www\include\like.php on line 13

Just so that you know, the table dislikes is currently empty, has 4 columns: id, pid, uid and like.

EDIT:

echo "SELECT * FROM dislikes WHERE pid = $post AND uid = $userid AND like = 1;";
$result = $mysqli->query("SELECT * FROM dislikes WHERE pid = $post AND uid = $userid AND like = 1;");
$row_cnt = $result->num_rows;

This is the php code. The echo from the first line outputs:

SELECT * FROM dislikes WHERE pid = 2 AND uid = 3 AND like = 1;
Nirav
  • 560
  • 1
  • 7
  • 17

1 Answers1

9

LIKE is a reserved word in SQL.

You have to delimit column names that conflict with SQL reserved words:

SELECT * FROM dislikes WHERE pid = $post AND uid = $userid AND `like` = 1;

But the explanation for your error is that mysqli_query() returned false instead of a valid query result. You need to check the function's return value to make sure it's a resource instead of false.

See also Reference - What does this error mean in PHP?

Community
  • 1
  • 1
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • 1
    or another alternative with bill's answer is to supply an alias on the table and use that along with the column name, eg. `FROM dislikes d WHERE d.like = 1` – John Woo Sep 14 '13 at 10:15
  • 1
    @491243, good tip. In fact, you don't even need a table alias. `dislikes.like = 1` would work too. – Bill Karwin Sep 14 '13 at 18:46