I have a query to a table where I am passing arguments and the result of this is that it returns all the stored items that meet the parameters
public function obtenerSimilares($cod, $cat){
$db = new Connection();
$query = "SELECT nombre, cantidad, codigo, id_cat FROM articulo WHERE codigo <> '$cod' AND id_cat = '$cat'";
$res = $db->query($query);
$datos = [];
// condicional
}
So far I have the following condition which helps me to know if I have a single result tuple or several to be able to save them.
if ($res->num_rows > 1) {
while ($row = $res->fetch_assoc()) {
$datos[] = [
'codigo' => $row['codigo'],
'nombre' => $row['nombre'],
'id_cat' => $row['id_cat'],
];
}
} else {
$datos[] = $res->fetch_assoc();
}
$cat
There will be times when the and the are not met $cod
(the variable cat is the identifier of the category and the cod is the identifier of the article code) and therefore I get a null response but the problem is that I have a container where I am going to show the results, only that this container has a display: none;
in its css and that is the reason why I want to implement a conditional that tells me if the result is a null so that when reading the script I keep it display
with the value determined or otherwise change it to display: block
. I have thought of another option which is to let the container have an display: block;
at every time and not control when the result of the query is NULL
.
Note: through the api fetch()
I am reading the content of $datos
and since it is an array in the .then()
defined ones, I parse it so that it has the JSON format so that it is easier to go through it and display the content of each object.
.then(res => res.json())
.then(dataS => {
// console.log(JSON.stringify(dataS));
Here I want to implement document.getElementById('contenedor').style.display ="block";
or otherwise create in css a class that has the display: block;
to add it to the class list of the container withdocument.querySelector('.contenedor').classList.add('visible');
How do I detect in php if the result of the query is NULL
?
The result of the query cannot be
null
, the only two possibilities are:fetch()
I guess what you really mean is knowing that the query returned no results and you shouldn't worry about that, just define an array and fill it with the information obtained:
Finally, in Javascript you check the number of rows received: