Show MySQL Query Result with Echo (PHP)

-1

I have a script in PHP and MySQL that has a query responsible for counting how many records (clients) are inserted into one of the tables in my database. However, I need to show the amount of records (clients) in the table with the echo command, but I do not know how to do it. Below is my code:

<?php

    include_once 'conexao.php';

    $sql = $dbcon->query("SELECT COUNT(*) FROM tbClientes");

?>

I tried to use echo $sql; but it did not work.

    
asked by anonymous 25.12.2018 / 18:37

1 answer

1

data for connection.php

$hostname="localhost";  
$username="USUARIO";  
$password="SENHA";  
$db = "Nome_DB";
  • With PDO

    $dbcon = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
    
    $sql = $dbcon->query("SELECT COUNT(*) FROM tbClientes");
    
    $total = $sql->fetchColumn();
    
    echo $total;
    
  • With mysqli

     $dbcon = new mysqli($hostname, $username, $password, $db);
    
     $sql = $dbcon->query("SELECT COUNT(*) FROM tbClientes");
    
     $row = $sql->fetch_row();
    
     echo $row[0];
    
25.12.2018 / 19:35