Hi, I'm doing a php function to show data from the DB, I have a DB connection variable outside of a function, and the query inside a function, so that in the function 'read' the variable uses global
<?php
$conexion = new mysqli("","","","");
if (!$conexion) {
die("Error al conectar con la base de datos: ".$conexion->connect_error);
}
function mostrar_datos(){
global $conexion;
$result = $conexion->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
//mostrar datos
}
}else{
echo "No hay mensajes";
}
}
$conexion->close();
?>
<div id="chat">
<?php echo mostrar_datos(); ?>
</div>
The strange thing is that I printed 'No messages' when there is data in the DB. And whose error I think is due to global $ connection.
Thank you.