ERROR Could not fetch mysqli and mysqli_fetch_assoc () expects parameter 1 to be mysqli_result

0

GIVE ME THIS ERROR:

  

Warning: mysqli :: query (): Could not fetch mysqli in C: --- on line 67   Warning: mysqli_fetch_assoc () expects parameter 1 to be mysqli_result,   null given in C: --- on line 69

        <?php

if (isset($_GET['cat']))    {
        //Seleccionamos lo filtrado por el numero de categoria:
$sql = "SELECT * FROM detall_productes WHERE codi_categoria =".$_GET['cat'];
}else{
        //Seleccionamos todo de nuestra BD
$sql = "SELECT * FROM detall_productes";
}

    //Realizamos la consulta gracias a la sentencia SQL
$result = $conn->query($sql);

while ($row= mysqli_fetch_assoc($result)){
        //relacionamos HTML con BD

//<TD> FILA DE DATO
echo '<tr> 
                            <td class="align-middle"><img src="images/productes/'.$row['imatge'].'" class="img-thumbnail mr-2" style="height: 50px;" />'.utf8_encode($row['nom']).'</td>
                            <td class="align-middle">'.utf8_encode($row['nom_categoria']).'</td>
                            <td class="align-middle text-right">'.$row['preu'].' €</td>
                                <td class="align-middle">

                                <form class="form-inline" action="carrito.php" method="post">
                                    <div class="form-group">
                                        <input type="hidden" name="codi" value="'.$row['codi'].'" />
                                        <input type="number" class="form-control form-control-sm mr-2" name="quantitat" min="1" value="1" style="width: 50px;" />
                                    </div>
                                    <button type="submit" class="btn btn-primary"><i class="fas fa-cart-plus"></i></button>
                                </form>
                            </td> 
                        </tr>';

}   
?>
                    </table>
                </div>
            </div>
        </div>
    </body>
</html>
    
asked by Vdefilosodia13 19.05.2018 в 11:46
source

1 answer

0

The error indicates that this call: $result = $conn->query($sql); is not returning a set of results, but NULL, either because $conn is null, or because the query has an error.

It is convenient that you evaluate the state of the variables that intervene in the code, and that you write a controlled code that reports the situation.

Try doing it this way. In the code that follows I have also applied a recommendation from the PHP Manual that says that you should not mix styles in mysqli, you had a mixture between procedural style and object oriented style. I have applied this last style in everything, the code is more readable and more modern in that way.

I hope it serves you.

if (isset($_GET['cat'])) {
    //Seleccionamos lo filtrado por el numero de categoria:
    $sql = "SELECT * FROM detall_productes WHERE codi_categoria =".$_GET['cat'];
}else{
    //Seleccionamos todo de nuestra BD
    $sql = "SELECT * FROM detall_productes";
}

if ($conn){
    //Realizamos la consulta gracias a la sentencia SQL
    $result = $conn->query($sql);

    if ($result = $conn->query($sql)) {

        /* obtener un array asociativo */
        while ($row = $result->fetch_assoc()) {

            /*
            *Aquí
            *toda la parte
            *HTML
            *de tu código
        */


        }//Esta llave cierra el while

        /* liberar el conjunto de resultados */
        $result->free();
    }else{

        echo "Error en la consulta: ".$conn->error;
    }

}else{

    echo "La conexión es nula, revise sus credenciales de conexión";

}
    
answered by 19.05.2018 в 13:25