Nirav
Nirav

Reputation: 570

This MySQL query doesn't seem to be working

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;

Upvotes: 2

Views: 102

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562250

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?

Upvotes: 9

Related Questions