Show Mysql query result with Echo (PHP)

Asked

Viewed 1,420 times

-2

I have a script in PHP and Mysql that has a query responsible for counting how many records (clients) are inserted in one of the tables of my database. However, I need to show the amount of records (clients) in the table with the echo command, but I don’t 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 didn’t work out.

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;
    
  • Mysqli

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.