MR KHAN COMUTI Leader
MR KHAN COMUTI Leader

Reputation: 55

Counting records using SQL and returning in PHP

I am trying to count the number of records I have in a certain. This is the SQL query I have been using and I have tried to echo $result, but nothing would print.

$result = $mysqli->query("SELECT COUNT(*) FROM barber_queue ");
echo $result;

I have also tried this code which managed to return something, however not in the format I would like

$result = $mysqli->query("SELECT COUNT(*) FROM barber_queue ");
$row = mysqli_fetch_row($result);
print_r($row);

This returns:Array ( [0] => 6 ) While 6 is the correct amount of rows, I would just like the integer on its own as I want to use this amongst HTML to relay some information back to visitors.

Also this piece of code

$result = $mysqli->query("SELECT COUNT(*) FROM barber_queue ");
    print_r($result);

returns on the screen. :

mysqli_result Object ( )

Any Help is appreciated.

Upvotes: 1

Views: 56

Answers (2)

Chibuzo
Chibuzo

Reputation: 6117

I added an alias "num" to the COUNT() keyword, the accessed the $row variable as an array();

$result = $mysqli->query("SELECT COUNT(*) num FROM barber_queue ");
$row = mysqli_fetch_assoc($result);
echo $row['num'];

Upvotes: 1

Reflective
Reflective

Reputation: 3917

you are missed mysqli_fetch_row(...);. you are dumping the system mysql_query object, which can not be access by print_r so you have this dump

Upvotes: 0

Related Questions