How to set a statement so that it does not return NULL values having two conditions in PHP?

0

I have the following code to consult the database:

<?php
include_once '__conexion.php';
require "funciones/JSON.php";
?>
<?php
  $cedula = 111111; //No existe
  $estado=1; 
   //-- Obtener Ultimo ID de sancion registrado para ese sancionado --//
  $stmt = $conexion->prepare("SELECT max(id_omd) FROM sanciones WHERE cedula_sancionado = ? AND estado_sancion_id = ?");
  var_dump($stmt)."<br>";
  $stmt->bind_param("ii", $cedula, $estado);
  $stmt->execute();
  $stmt->store_result();
  $cantidad=$stmt->num_rows;
  var_dump($cantidad);
  $stmt->bind_result($ultimoregistro);
  $stmt->fetch();

  if ($stmt->num_rows === 0) {
  echo "No registro";
  } else {
  echo "Si hay registros";
  }

The problem : In the database there is NO record with this data Making the query in phpMyAdmin you get a NULL value:

max(id_omd)
NULL

When I use var_dump($canrtidad) ; returns int(0) .

But the query continues as if there are records, invalidating $stmt->num_rows===0

    
asked by Victor Alvarado 04.05.2017 в 17:14
source

4 answers

1

The use of num_rows to know if there are real data in a set of results is not very reliable, although it is the most common practice in many answers, unfortunately ...

This case is typical of this, because it asks about the number of rows there are and, in effect, there is a row, only that the data in it is null .

  

Making the query in phpMyAdmin you get a NULL value:

max(id_omd)
NULL

No data , or rather, there is a row without data or with null data .

Then, it is safest to ask for the data itself.

If you have data stored here:

  $stmt->bind_result($ultimoregistro);

ask for them :

I have changed the order, but you can also ask in negative ...

  if ($ultimoregistro) {
  echo "Si hay registros";
  } else {
  echo "No registro";
  }

The thing would look something like this:

  $cedula = 111111; //No existe
  $estado=1; 
   //-- Obtener Ultimo ID de sancion registrado para ese sancionado --//
  $stmt = $conexion->prepare("SELECT max(id_omd) FROM sanciones WHERE cedula_sancionado = ? AND estado_sancion_id = ?");
  var_dump($stmt)."<br>";
  $stmt->bind_param("ii", $cedula, $estado);
  $stmt->execute();
  $stmt->store_result();
  $cantidad=$stmt->num_rows;
  var_dump($cantidad);
  $stmt->bind_result($ultimoregistro);
  $stmt->fetch();

  if ($ultimoregistro) {
  echo "Si hay registros";
  } else {
  echo "No registro";
  }

Note

The one that returns NULL is something else, it may be due to an error in the data or in the SQL query or in which simply the field of SELECT that fulfills the condition has no data and is a column that accepts null.

    
answered by 04.05.2017 / 18:31
source
1

You should check what type of value returns num_rows since if you are returning '0' as String and you compare it with 0 as int the condition is not met when using === since thus you are saying that in addition to having the same value, they must be of the same type, however if you use == just verify that they have the same value regardless of the type of each one of them.

    
answered by 04.05.2017 в 17:46
1

There is another possible solution:

if (!empty($stmt->num_rows)) { // OJO con el ! para cambiar el resultado de empty echo "Si hay registros "; } else { echo "No registro"; }

That way you can validate a 0 or a NULL.

    
answered by 04.05.2017 в 20:24
0

Use the check in this way:

  

A solution

  if ($stmt->num_rows == 0) {
    echo " No registro";
  } else {
    echo "Si hay registros";
  }
  

Another solution

  if ($stmt->num_rows > 0) {
    echo "Si hay registros ";
  } else {
    echo "No registro";
  }
    
answered by 04.05.2017 в 18:01