I have this error Warning: mysqli num rows () expects parameter 1 to be mysqli result, boolean given

2

I have implemented a notification system, but I made a small addition to the connection and I get this error:

Warning: mysqli num rows() expects parameter 1 to be mysqli result, boolean given

But I do not know what it means or what I would like to suggest.

<?php
$conexion= mysqli_connect("localhost", "root", "", "antetodopopayan");
    $count = 0;
    $sql = "SELECT * FROM informacion WHERE estado = 0";
    $result = mysqli_query($conexion, $sql);
    $count = mysqli_num_rows($result);

if(!$conexion){
    echo "error";
}else{

}

?>
    
asked by Stiven 24.12.2018 в 17:06
source

1 answer

1

Well according to the comments area, you mention wanting to do an accountant for that query. so you should use an aggregation function like COUNT() given the above I propose that your code look like this:

<?php

$conexion = new mysqli("localhost", "root", "", "antetodopopayan");
$consulta = $conexion->query("SELECT COUNT(*) as T FROM informacion WHERE estado = 0");
$fila = $consulta->fetch_assoc();
echo $fila["T"];

OBSERVATIONS

  • As notes, best use an object-oriented style with new mysqli
  • As I'm going to count the rows that enter a condition, I put an alias to that same count, in this case T
  • In a variable $fila I assign the query to use the fetch_assoc() method
  • As a final step as I try to print the number of rows that enter given the condition in WHERE I do an echo of said variable, but accessing the key that I place that is T
  • UPDATE

    If you want to manage the state of the connection then your code should look like this

    <?php
    
    $conexion = new mysqli("localhost", "root", "", "antetodopopayan");
    
    if($conexion->connect_errno){
        return $conexion->connect_error;
    }else{
        $consulta = $conexion->query("SELECT COUNT(*) as T FROM informacion WHERE estado = 0");
        $fila = $consulta->fetch_assoc();
        echo $fila["T"];
    }
    
        
    answered by 24.12.2018 / 17:33
    source