The global variable does not work for me

0

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.

    
asked by cat_12 13.09.2018 в 11:18
source

2 answers

2

Apart from not having the variable $ sql declared as commented by @Jakala, you are opening the connection with the database, then you define the show_data function but then you close that connection. When it is time to call the show_data function, the connection is already closed. Try changing your code for this:

<?php
  function mostrar_datos(){
      $conexion = new mysqli("","","","");
      if (!$conexion) {
        die("Error al conectar con la base de datos: ".$conexion->connect_error);
      }
      $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>
    
answered by 13.09.2018 в 12:08
0

Thank you very much the error was that that closed the connection, but I do not understand ... Is not it executed line by line ?, because if it is, the connection, the function would be executed and finally it would be necessary to close the connection. Or is it that it executes first the function outside and then the functions that are there?

How is that?

Solution:

<?php
  $conexion = new mysqli("sql113.260mb.net","n260m_20445422","jyEsRoXZ","n260m_20445422_GTE");
  if (!$conexion) {
    die("Error al conectar con la base de datos: ".$conexion->connect_error);
  }
  function mostrar_datos(){
    global $conexion;
    $mostrardatossql = "SELECT * FROM chat_do";
    $result = $conexion->query($mostrardatossql);
    $listas = $result->num_rows;
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
          echo "<div id='chat_do2'>";
          echo "<div id='datos-chat-do'>";
          echo "<table>";
          echo "<tr>";
          echo "<td id='mensaje1'>".$row['mensaje']."</td>";
          echo "<td rowspan='2' id='mensaje-info'>".$row['clase']." ".$row['hora']."<br>".$row['fecha']."</td>";
          echo "</tr>";
          echo "<tr>";
          echo "<td>".$row['multimedia']."</td>";
          echo "</tr>";
          echo "</table>";
          echo "</div>";      
          echo "</div>";
          echo $result->num_rows;
        }
   }else{
     echo "No hay mensajes";
   }        
 }

? >

    
answered by 13.09.2018 в 12:23