Getting just the number of rows using Mysqli and PHP

2082 views php

I want to get how many (possibly 0) times a particular number occurs in a particular column. I set the number in $contact_client_ID then do the SELECT query below.

$sql = "SELECT * FROM t_contacts WHERE contact_client_ID ='$contact_client_ID'";
$result=(mysqli_query($link, $sql));
$count_result= mysqli_num_rows($result);

echo "contact client ID $contact_client_ID xxxxx $count_result";

Instead of $count_result containing the number I want, it contains a result made up of the number I want and the contact_client_ID joined together and the result doesn't seem to be usable as a number in any following code.

So, if $contact_client_ID = 50 and there are 2 occurrences of it in the table, the output I get is:

contact client ID 50 xxxxx 250

I've looked at the manuals and examples all over the place (including here) and I can't see what I'm doing wrong.

answered question

SELECT contact_client_ID, count(*) FROM ... GROUP BY contact_client_ID

You must have an echo 50 somewhere in your code that displays the stray 50.

Why are you wrapping (mysqli_query($link, $sql)); in parenthesis (brackets)?

2 Answers


$sql = "SELECT count(contact_client_ID) as total FROM t_contacts WHERE contact_client_ID ='$contact_client_ID'";
$result=mysqli_query($link, $sql);
$count_result= mysqli_fetch_row(result);

echo "contact client ID $contact_client_ID xxxxx $count_result['total']";

posted this

Try to echo only the count_result, I think you are concatenating all the number together!

echo $count_result;

posted this

Have an answer?


Please login first before posting an answer.