0

I have posted a code I am working on and the result in array. please tell me how to choose only a part of the result from that array ? code

$sql = 'SELECT status, phone, COUNT(*) AS count FROM people WHERE phone=9876543210 GROUP BY phone, status';
  if($result = mysqli_query($link, $sql)){
      if(mysqli_num_rows($result) > 0){
      while($row = mysqli_fetch_array($result)){
    print_r(array_count_values($row));
         }

Result

Array ( [1] => 2 [9876543210] => 2 [14] => 2 ) Array ( [2] => 2 [9876543210] => 2 [10] => 2 ) Array ( [3] => 2 [9876543210] => 2 [5] => 2 ) Array ( [4] => 2 [9876543210] => 2 [1] => 2 )

How to choose and display only the number 14? or only the number 5? I want to ad the number 14 to a $value. like echo $value; should result the number 14. Update: I am not trying just to filter the results from the database, the usage is to count certain values. I wan to display like "14 of your orders are approved, 5 of them cancelled, etc.."

1 Answer 1

0

Why simple don't use echo like:

echo $row['nameofdbvalue'];

Try to see that SO topic : SO LINK


For answer at your comment:

If your database is for example:

id - name
1  - Jason
2  - Micheal

You query with a simple select and fetch result like:

if($result = mysqli_query($link, $sql)){
   if(mysqli_num_rows($result) > 0){
     while($row = mysqli_fetch_array($result)){
      echo $row['name'];
    }
  }
}

This code will print Jason and Micheal.

if you want only one of that you can change your query with WHERE id='1' and echo will output only Jason


you need to fetch row like that so:

$sql = "select count(*) from table limit 100";
$count = $mysqli->query($sql)->fetch_row()[0];
echo $count;
Sign up to request clarification or add additional context in comments.

4 Comments

I am sorry, I didnt understand, can you show an example ?
thanks for the reply. But u misunderstood, because I was not very clear with my question. I am not looking for a way to filter my database results, I have already done that and got results, now I want to show the count of that results. these counts are displaying in array.
i change again!
thanks for the time, but this wont work for me. but I am looking for help with "foreach".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.