-2

My php query look like:

<?php 
$raw_results = mysql_query("SELECT Operator, Data_przegladu, IFNULL(COUNT( Operator ),0) AS operator_count 
                            FROM przeglad 
                            WHERE MONTH(Data_przegladu) = 1 
                              AND Operator = \"Adrian Pikus\" 
                            HAVING (COUNT( Operator ) > 1)") or die(mysql_error());       
$row = mysql_fetch_array($raw_results);
echo $row['operator_count'];
?>

The result is NULL but i want the result to be 0. Somebody can help me? I use PHP 5.2 and i can't update it

Dharman
  • 30,962
  • 25
  • 85
  • 135
Blimer
  • 45
  • 9
  • 1
    *The result is NULL* Do you mean that the query returns empty rowset? – Akina Sep 29 '20 at 11:16
  • 1
    If the query didn't return any rows, then `operator_count` is an undefined index. If the query ran successfully, you should be hitting the `IFNULL()` function. – Qirel Sep 29 '20 at 11:20
  • **Warning:** `mysql_*` extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0. Instead, either the [mysqli](https://www.php.net/manual/en/book.mysqli.php) or [PDO_MySQL](https://www.php.net/manual/en/book.pdo.php) extension should be used. See also the [MySQL API Overview](https://www.php.net/manual/en/mysqlinfo.api.choosing.php) for further help while choosing a MySQL API. – Dharman Sep 29 '20 at 11:29
  • You have already asked [this question](https://stackoverflow.com/questions/64116452/how-to-return-0-when-the-query-return-null) today. Please do not abuse the system to reask the question. – Dharman Sep 29 '20 at 11:30
  • When would `COUNT( Operator )` be NULL? You can't check for NULL on a function that always returns an integer. What exactly are you asking about? – Dharman Sep 29 '20 at 11:32

2 Answers2

-1

I think the null coalescing operator should do the trick:

For php 7 and above

echo $row['operator_count'] ?? 0;

Works for all versions

echo $row['operator_count'] ? $row['operator_count'] : 0;

Both will print $row['operator_count'] if it is not null else 0;

Tushar
  • 665
  • 5
  • 11
  • 1
    That won't work on PHP 5.2, which is what OP is using. – Qirel Sep 29 '20 at 11:16
  • I have edited my answer, please have a look. – Tushar Sep 29 '20 at 11:23
  • echo $row['operator_count'] ? $row['operator_count'] : 0; - it work! Thank you! – Blimer Sep 29 '20 at 11:39
  • 1
    FYI - that would cause *undefined index* notices. Use `isset()`. – Qirel Sep 29 '20 at 11:42
  • These two are not the same. The second one would cause an error as Qirel pointed out. Besides, the query returns the outcome of `COUNT( Operator )` which should be defined . The SQL needs to be corrected in this case. OP is using the aggregate function in the HAVING clause and this probably produces 0 results for them. IF you wanted to check for that in PHP you would need to use `if($row)` not the ternary – Dharman Sep 29 '20 at 11:58
-1
$row = is_null($row) ? 0 : $row;

You can use is_null

Jerson
  • 1,700
  • 2
  • 10
  • 14