Hello friends! I have a small problem and it is the following:
I want to condition a 'Null' row in MySql but I don't know how to do it. This is my PHP code:
$idUsuario = $_SESSION['id_usuario'];
$resultados = mysqli_query($mysqli,"SELECT * from usuarios where numero is null AND id = idUsuario");
if($resultados= Null){
require 'need_phone.php';
}else{
require 'have_phone.php';
}
What I want is that if the row is ' Null ' it brings me the file " need_phone.php " otherwise it brings me the file " have_phone.php "
I thank you in advance!
I think the code could be slightly modified to better deal with the result of type
mysqli_result
.Note that the result of the query would not be
null
as assumed in the original code since that method returns one of the following values according to the documentation :FALSE
: Facing an error.mysqli_result
: For successful queries of type SELECT, SHOW, DESCRIBE, or EXPLAIN.TRUE
: For other satisfactory inquiries.Apart from the mentioned syntax error.
I think it would be easier if you handle it like this:
COUNT
, it will return a number greater than or equal to 0 depending on whether at least one record with these conditions is foundmysqli_num_rows()
to get the number returned by the previous queryif
we verify if the value that it returned to us is 0, in that case it is that there is no record and we require the fileneed_phone.php
or otherwise the filehave_phone.php
Code
Why the change in code structure?
Well, you are trying to compare the result of the query as if it has results, it returns them and if it does not find records
NULL
, it generates which is not the case; in fact.If you print the query directly like this:
You will get a result like this:
That's why you don't get the expected behavior, since as the result in your conditional doesn't give
NULL
then it is passed to the code block of theELSE
The code would be as follows if you wanted to compare and know if it is null.