Search code examples
phpmysqlcomparison-operators

Best comparison operator when using COUNT in mysqli query


Doing a mysqli query and COUNTing the results, is there a preferred comparison operator & string enquoting to use?

For example

$query = mysqli_query($connect, "SELECT COUNT(`user_id`) FROM `users` WHERE `user_color` = '{$color}'");
$result = mysqli_fetch_assoc($query);
if ($result['COUNT(`user_id`)'] != 0){
 -- then do something....

My question is, what are the implications of using:

if ($result['COUNT(`user_id`)'] != '0'){

versus

if ($result['COUNT(`user_id`)'] != 0){

Solution

  • Both are same, php takes care of string and number.

    Internally functionality of php interpreter: If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.