Anuraag Kapoor Anuraag Kapoor -4 years ago 93
MySQL Question

Display Count(*) in PHP

I want to display the number of rows with the word in a string of a column having "LIV" in it . This is my code. When i run query in phpmyadmin the result shows I have 2358 rows but when run in php it says only 1 row .
Here is the Code :
I am using mysqli_* functions , so please suggest accordingly .

<?php
$sql = mysqli_query ($connect,"SELECT count(*) FROM w2 WHERE Statut_Cde LIKE '%LIV%';");
$result = mysqli_num_rows($sql);
$my = $result['count(*)'];
?>

<h2>
<b> Current Number of Repair Orders : <?php echo $result; ?> </b>
</h2>


Thanks in advance !

Answer Source

Your are getting the total of all by count, this is a grouped result. mysqli_num_rows gives the total rows, but because it's one row you get 1 as result.

This will solve it:

<?php $sql = mysqli_query ($connect,"SELECT * FROM w2 WHERE Statut_Cde LIKE '%LIV%';"); $result = mysqli_num_rows($sql); ?> <h2><b> Current Number of Repair Orders : <?php echo $result; ?> </b></h2>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download