Problem with COUNT (*) Does not return data (PHP AND MYSQL) [closed]

3

I try to count the amount of invoices in the database:

//conexion:
    [email protected]_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    if(!$con){
        die("imposible conectarse: ".mysqli_error($con));
    }
    if (@mysqli_connect_errno()) {
        die("Conexión falló: ".mysqli_connect_errno()." : ". mysqli_connect_error());
    }

// Connection in this file:

require_once ("config/db.php");//Contiene las variables de configuracion para conectar a la base de datos
require_once ("config/conexion.php");//Contiene funcion que conecta a la base de datos

    $query = "SELECT COUNT(*) AS total FROM 'facturas'";

    if ($result) {
        $result = mysqli_query($con, query); 

        $row = mysqli_fetch_array('MYSQLI_ASSOC'); 
        echo $row['total'];
    } else {
        echo "Sin datos que mostrar";
    }

The case that comes out of the if and tells me "no data to show".

    
asked by Javier Avila Fernandez 27.04.2018 в 17:38
source

2 answers

5

Your code should be in the form:

  • You can place the name of the table without the single quotes.
  • With     respect to 'mysqli_fetch_array' must have two parameters, and     you are having one and the wrong way.

Code:

$query = "SELECT COUNT(*) AS total FROM facturas";
$result = mysqli_query($con, $query); 

if ($result) {
    $row = mysqli_fetch_array($result ,MYSQLI_ASSOC); 
    echo $row['total'];
} else {
    echo "Sin datos que mostrar";
}
  

Reference: link

    
answered by 27.04.2018 / 17:59
source
3

The truth is that I have a bit rusty php but, you are asking for $ result before giving it value. In addition, you missed the '$' before query. Try this way:

$query = "SELECT COUNT(*) AS total FROM 'facturas'";
$result = mysqli_query($con, $query); 

//if ($result) {
if (mysqli_num_rows($result) > 0)
    $row = mysqli_fetch_array('MYSQLI_ASSOC'); 
    echo $row['total'];
} else {
    echo "Sin datos que mostrar";
}
    
answered by 27.04.2018 в 17:40