PHP MySQL SELECT statement return null

Amir Meimari

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:

select statement null

Mickaël Leger

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.

edited at
0

Comments

0 comments
Login to comment

Related

PHP MySQL imbricate SELECT statements return null

php mysql database select statement

MySQL Return the first non-NULL value from the list of the column in select statement with column name

Mysql SELECT statement to return only rows where/if a given number of cols are not null

nodejs mysql query return value of select statement

Run Second Select statement if the first SQL statement return NULL(or nothing)

Php Phalcon - Select statement does not return anything

PHP MYSQL SELECT Returns NULL

Mysql select ignores null values on return results

Ignore Insertion if select subquery return null - Mysql

Php Regex to find if string is mysql select statement

PHP variable in MySQL [ Select OR ] Statement not executing

Utilizing two php variables in mysql select statement

Mysql select statement using php array

PHP MySQL change the SELECT statement with a href or a button

mysql select query result is null so how can i return default value in php

Select statement to return one value or another one if the first one is null

Select statement in hive return some columns with null value

php return statement in if statement

Mysql request return null Controller PHP

Can MySQL 8.0 return array of JSON objects in Select statement?

Return a select statement result

PHP PDO close connection set to null but return statement

Mysql select only data whos not null in php

MySQL select statement result in after delete trigger always null

Return MySQL Error Code and Custom Response using Prepared Statement PHP

Concat statement in mysql with PHP causes mysqli_query to return false

Return \' within a MySQL statement

Mysql IF statement return issues