I have the following code, that when I enter a value, it does not return anything. I don't know where the error is, I've tried everything.
$json=array();
if(isset($_GET["cat"])){
$cat=$_GET["cat"];
$conexion = new mysqli($hostname_localhost, $username_localhost, $password_localhost, $database_localhost);
$consulta='select nombre,descripcion,facebook,horario,direccion,ruta_i,ruta_i6,ruta_i2,ruta_i3,ruta_i4,ruta_i5 from negocio inner join categoria on negocio.cat = categoria.id where cat =' . $cat;
$resultado=mysqli_query($conexion,$consulta);
while($registro=mysqli_fetch_array($resultado)){
$json['negocio'][]=$registro;
}
mysqli_close($conexion);
echo json_encode($json);
}
If I enter the value 1, it should return the corresponding data, but it does not return anything. If I enter a value that does not correspond, for example 2, it returns an empty arrays [].
array(1) { ["business"]=> array(2) { [0]=> array(11) { ["name"]=> string(15) "Alma de Lagunas" ["description"]=> string(124) "In our breakfast buffet you will try homemade delicacies, homemade jams, a variety of infusions and natural juices. " ["facebook"]=> string(44) " https://www.facebook.com/almadelagunashotel/ " ["schedule"]=> string(15) "Always open" ["address"]=> string(16) "Av. Irigoyen 152" ["ruta_i"]=> string(55) " http://emiibarra6 .com/DBremota/images/almadelagunas.jpg " ["ruta_i6"]=> NULL ["ruta_i2"]=> string(56) " http://emiibarra6.com/DBremota/images/almadelagunas2.jpg " ["ruta_i3"]=> string(56) " http://emiibarra6.com/DBremota/images/almadelagunas3.jpg " ["ruta_i4"]=> string(56) "http://emiibarra6.com/DBremota/images/almadelagunas4.jpg " ["ruta_i5"]=> string(56) " http://emiibarra6.com/DBremota/images/almadelagunas5.jpg " } 1 => array( 11) { ["name"]=> string(13) "Hotel Guamin�" ["description"]=> string(13) "Hotel Guamin�" ["facebook"]=> string(38) " https:/ /www.facebook.com/hotelguamini/ " ["schedule"]=> string(15) "Always open" ["address"]=> string(17) "San Martin y Alem" ["ruta_i"]=> string (54) " http://emiibarra6.com/DBremota/images/hotelguamini.jpg " ["ruta_i6"]=> NULL ["ruta_i2"]=> string(55) "http://emiibarra6.com/DBremota/images/hotelguamini2.jpg " ["ruta_i3"]=> string(55) " http://emiibarra6.com/DBremota/images/hotelguamini3.jpg" ["path_i4"]=> NULL ["path_i5"]=> NULL } } }
Try this controlled code, it will tell you the status of the json with the
var_dump
at the end.Fixes
error
to the final data that will help you determine if there were errors and what they were.Safety Note
Your way of querying here is not secure, the code could be exposed to SQL injection attacks. I have not made that correction so as not to confuse you too much, but it is good that you take it into account, document yourself about that risk and use prepared queries for these cases.
I hope it helps you.