Display Count(*) in PHP

1.7k Views Asked by At

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 !

2

There are 2 best solutions below

0
On BEST ANSWER

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>

1
On

Try this

$sql = mysqli_query ($connect,"SELECT count(*) as cnt FROM w2 WHERE Statut_Cde LIKE '%LIV%';");
$result = mysqli_fetch_assoc($sql);
$my = $result['cnt'];