I want to select data from MySQL
with a a where condition.
I'm sure the condition is false that's why it shouldn't return any data or any row but it returns null. I checked it with $stmt2->num_rows > 0
and this condition is true.
How can i prevent this from happening?
How my code suppose to work : this condition should be false for the first time and be true after I insert some data.
I think it's for SUM()
that is used in select statement. How can I do that?
$q2="SELECT SUM(liked), SUM(disliked) FROM like_dislike WHERE post_id=?";
$stmt2 = $conn->prepare($q2);
$stmt2->bind_param('i', $post_id);
$stmt2->execute();
$stmt2->store_result();
$stmt2->bind_result($like_status, $dislike_status);
if ($stmt2->num_rows > 0 ) {
$stmt2->fetch();
}
else {
$like_status =0;
$dislike_status =0;
}
My select statement result in phpmyadmin:
According to the documentation :
SUM([DISTINCT] expr)
Returns the sum of expr. If the return set has no rows, SUM() returns NULL. The DISTINCT keyword can be used to sum only the distinct values of expr.
If there are no matching rows, SUM() returns NULL.
So you will always have NULL as result if you have no data.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments