Result of a NULL query


Sorry I have been trying to make umeo next select:

$sql =mysqli_query ($mysqli, "SELECT hour_out FROM registro_lista WHERE num_c="316115377");

I know from phpMyAdmin that the field I'm looking at is NULL

But I want to finalize it in an If in the following way:

If (is_null($sql)) {



But it goes straight to the else, when the query should give Null, am I doing something wrong ??

asked by Raúl Arcos 17.07.2018 в 05:10

2 answers


Something that helps a lot is the Return Values section of the PHP Manual, so we know how to evaluate the results of the functions.

In the case of mysqli_query , which is the function you're using, The Manual says the following in that section :


Returns FALSE in case of error. If a query of type SELECT ,    SHOW , DESCRIBE or EXPLAIN is successful, mysqli_query() will return   a mysqli_result object. For other successful consultations    mysqli_query() will return TRUE .

So when you use mysqli_query you have three possibilities of result:

  • FALSE ,
  • an object mysqli_result o,
  • TRUE .

That means that here:

$sql =mysqli_query ($mysqli, "SELECT hour_out FROM registro_lista WHERE num_c="316115377");

the heat of $sql will never be NULL even in case the query fails and the variable $sql acquires the value FALSE , since FALSE is not equal to NULL .

If, as you say, what you want to evaluate is the value of the column hour_out that the query brings you, then you must recover that value by reading the object that returns mysqli_query in case of success.

There are several methods to read the objects mysqli_result , one of the most used is mysqli_fetch_array which returns the value in an associative array in which each key is the name of each column of SELECT :

$sql =mysqli_query ($mysqli, "SELECT hour_out FROM registro_lista WHERE num_c='316115377'");

$row = mysqli_fetch_array($sql, MYSQLI_ASSOC);
$hour = $row["hour_out"];

And then you can compare by:

if (is_null($hour)) {



Or simply by:

if ($hour) {



Or using a ternary operator:

$resultado = ($hour)  ?  "No es nulo"  :  "Es nulo";
echo $resultado;



Attention to your query. I had a syntax error. I've put it like this:    "SELECT hour_out FROM registro_lista WHERE num_c='316115377'" but   if the column num_c is of a numeric type, it is better that you remove the   single quotes that surround the value, putting it like this: "SELECT hour_out FROM registro_lista WHERE num_c=316115377"

answered by 17.07.2018 / 05:58

I tell you the following:

To be able to validate if the return of your query is actually NULL, you must do it with the is_null() function, but the variable must indicate the name in quotes of the column you want to validate if it is NULL or not.

I leave this example, what I did with PDO but it is only a matter of what you adapt to your needs


$conexion = new PDO('mysql:host=localhost;dbname=ls;port=3307', 'root', '*****');

$consulta = $conexion->prepare("select * from m");
$resultado = $consulta->fetch();

    echo "No tiene valor";
    echo "Tiene valor";

As you can see my if() is similar to yours only I indicate   between brackets the column on which you are going to be working that   for my example is "name" you just write only the column that you are   working


I commented that I make some observations to your code and could be as follows

$sql =mysqli_query($mysqli, "SELECT hour_out FROM registro_lista WHERE num_c = 316115377");
$result = mysqli_fetch_row($sql);

    echo "No tiene valor";
    echo "Tiene valor";

Because for example you are using the quotes wrongly because you use them to enter the number of your WHERE but then you do not close the quotes of the beginning

Since you are only going to return a column in your query, you should use mysqli_fetch_row()

answered by 17.07.2018 в 05:29