Error fetch_assoc () on boolean

2

I need to be able to modify a record using a table, but it happens that when I want to execute the action of the button, it sends me an error.

I had previously done a test with a table of 15 records or so and it did work, but when I tried to do this one that has more than a thousand records it showed me the error.

This is my table:

<div class="container">
    <table class="table display table-striped table-responsive">
       <thead style="color: #5acbf5">
        <tr>
            <th>Sinatura</th>
            <th>Titulo</th>
            <th>Autor</th>
            <th><a href="nuevo1.php"><button type="button" class="btn btn-info">Nuevo</button></a></th>

       </tr>
    </thead>

        <tbody>
    <?php 
                      include "conexion.php";
                      $sentencia = "SELECT * FROM books";
                      $resultado = $conexion->query($sentencia) or die (mysqli_error($conexion));
                      while($fila = $resultado->fetch_assoc()){
                        echo "<tr style='color: #7dce30'>";
                          echo "<td>"; echo $fila['Sinatura']; echo "</td>";
                          echo "<td>"; echo $fila['Titulo']; echo "</td>";
                          echo "<td>"; echo $fila['Autor1']; echo "</td>";

                          echo "<td><a href='modificar1.php?no=".$fila['ID']."'> <button type='button' class= 'btn btn-success'>Modificar</button></a></td>";

                          echo "<td><a href='eliminar.php?no=".$fila['ID']."'> <button type='button' class= 'btn btn-danger'>Eliminar</button></a></td>";
                        echo "</tr>";
                      }
    ?>
  </tbody>

I get the following error:

  

Fatal error: Uncaught Error: Call to a member function fetch_assoc () on boolean in C: \ xampp \ htdocs \ scripts \ operations \ modify1.php: 9 Stack trace: # 0 C: \ xampp \ htdocs \ scripts \ operations \ modify1.php (3): ProductInquiry ('1') # 1 {main} thrown in C: \ xampp \ htdocs \ scripts \ operations \ modify1.php on line 9

And in that part I have done:

<?php 
//modificar1.php
$consulta = ConsultaProducto($_GET['no']);

function ConsultaProducto($no_prod){
  include 'conexion.php';
  $sentencia = "SELECT * FROM books WHERE no='".$no_prod."' ";
  $resultado = $conexion->query($sentencia);
  $fila = $resultado->fetch_assoc(); //Aquí es donde me marca el error

  return[
    $fila['Sinatura'],
    $fila['Titulo'],
    $fila['Autor1']
  ];
}

Therefore, I do not know what to do ... I tried with array but it did not work either.

This part is divided into modificar1 and modificar2 , where the other is:

<?php //modificar2.php
ModificarProducto($_POST['no'], $_POST['Sinatura'], $_POST['Titulo'], $_POST['Autor1']);

function ModificarProducto($no, $id_prod, $nom, $descrip){
    include 'conexion.php';
    $sentencia="UPDATE books SET Sinatura='".$id_prod."', Titulo='".$nom."', Autor1='".$descrip."' WHERE no='".$no."' ";
    $conexion->query($sentencia) or die ("ERROR al actualizar datos");
}
    
asked by Vicky_96HV 08.02.2018 в 06:33
source

1 answer

2

The meaning of Fatal error: Uncaught Error: Call to a member function fetch_assoc() on boolean comes from not taking into account that $resultado could be worth false (a boolean value, not a mysqli resource) in case of an error in the call to query() .

A Boolean data has no properties or methods (like the method called fetch_assoc() that you try to use). Hence the message: "You can not call a member function of a Boolean data".

You can find out what is wrong with your code if you shield it correctly from error situations. In addition, you should use queries prepared to escape SQL characters (such as quotes) or, at least, use the mysqli::real_escape_string() to prepare the string to be inserted in a SQL query correctly:

function ConsultaProducto($no_prod) {
  /* Es preferible require sore include para que finalice la ejecución
      si no encuentra el archivo */
  require_once 'conexion.php';
  /* Para facilitar la lectura asigno a una nueva variable el contenido
      de $no_prod escapado con real_escape_string */
  $no_prod_sql = $conexion->real_escape_string($no_prod);
  /* Puede que $no_prod sea un número, por lo que se puede escapar
      o bien comprobar que sea un número válido (y no una cadena vacía) */
  $sentencia = "SELECT * FROM books WHERE no='$no_prod_sql'";
  $resultado = $conexion->query($sentencia);
  /* Si $resultado vale false es porque ocurrió un error en la consulta */
  if ($resultado === false) {
    die($conexion->error);
  }
  /* OJO: Si no hay ninguna fila que obtener ¡$fila valdrá false! */
  $fila = $resultado->fetch_assoc();
  return [
    $fila['Sinatura'],
    $fila['Titulo'],
    $fila['Autor1'],
  ];
}
    
answered by 08.02.2018 / 09:47
source