Fill a variable in PHP when a query is empty in MySQL

0

When doing a MySQL query in PHP the extracted does not throw anything at you so you assign a variable the extracted one does not take it as null or as zero even though you have created the variable as I do to assign a value to a variable in php for empty queries? I leave the code

$consulta = mysqli_query($conexion, "SELECT tanquenum from tanque WHERE tanqedo = 'Ocupado'")
        or die ("Error de consulta");
while ($extraido = mysqli_fetch_array($consulta)) {

       echo $extraido['tanquenum'];
       $p=$extraido['tanquenum'];
       echo gettype($p)."<br>";
}

the value of $p does not take any type of variable or is filled, I need to fill it to make another procedure when the query is empty

    
asked by Rob Robs 04.11.2018 в 20:40
source

2 answers

0

You can initialize it before the loop $p = 'loquequieras'; while ...

    
answered by 04.11.2018 в 20:43
0

(PHP) A simple solution would be to initialize the variable $ p with an empty value and then add a simple conditional, where you use the EMPTY function to verify that a variable is empty. That is, your code should be as follows:

$p = ""; //La variable inicia con vacío

$consulta = mysqli_query($conexion, "SELECT tanquenum from tanque WHERE 
tanqedo = 'Ocupado'") or die ("Error de consulta");

while ($extraido =  mysqli_fetch_array($consulta)) {
 echo $extraido['tanquenum'];
 $p=$extraido['tanquenum']; 
 echo gettype($p)."<br>"; 
}

/*Condicional simple para verificar que la variable aún está vacía, y, de ser 
así, le agregamos un valor por defecto*/

if(empty($p == "")){
  $p = 'CualquierValor'; //Agregas el valor que quieras
  echo gettype($p)."<br>"; 
}

To complete, I leave you link to the official PHP documentation for the EMPTY function.

PHP EMPTY Documentation

    
answered by 04.11.2018 в 21:12