-1

I have been trying this code for some time now, and I cant figure it out.

   <?php
$link = mysqli_connect("localhost", "h4g54453g5234", "23j4hbjh243v535", "3j45hv6j3h45v645");
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = 'SELECT COUNT(*) FROM users WHERE phone=9876543210 AND status=1';
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
        while($row = mysqli_fetch_array($result)){
echo "" . $row['COUNT(*)'] . "";                  
        }
        mysqli_free_result($result);
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);

?>

table sample enter image description here

The result of above code will be "4", because the column "status" only 4 data with the number "1".

now I can use echo $row['COUNT(*)']; to display the number "4".

But, I want to show results like following echo "$status1; (which will show "4") echo "$status2; (which will show "4") echo "$status3; (which will show "2")

How can I do that? Using the above code, I can only show the result of status column with value 1.

4
  • SELECT status, COUNT(*) AS count FROM people WHERE phone='9876543210' GROUP BY status Commented May 12, 2020 at 4:28
  • @Nick ,this shows the results of all status, can you tell me how to echo only the count of status "3" for example ? Commented May 12, 2020 at 4:51
  • SELECT COUNT(*) FROM people WHERE phone='9876543210' AND status=3 Commented May 12, 2020 at 5:28
  • @Nick, thanks for the reply, that really works. But I also want to show the results of status 1 and status 2 at the same page, just like status 3. how can I shows all at the same time, but like echo $status1; echo $status2; echo $status3; etc. Commented May 12, 2020 at 5:49

1 Answer 1

0
SELECT status, phone, COUNT(*) AS count FROM people GROUP BY phone, status

SELECT status, phone, COUNT(*) AS count FROM people WHERE status=3 GROUP BY phone, status

SELECT status, phone, COUNT(*) AS count FROM people WHERE phone='9876543210' GROUP BY phone, status
Sign up to request clarification or add additional context in comments.

13 Comments

this shows the results of all status, can you tell me how to echo only the count of status "3" for example ?
Use where clause.
but if I user WHERE status=3, it shows the results from multiple phone n umbers. I only want to result the statuses of one phone number, which is "9876543210". and not only one status, I want to display status of all 0,1,2 and 3
So add WHERE phone='9876543210' condition
@ Pajfs An, yes, that is what I am doing now. but the result is showing as an array. the result is "141051". 14, 10,5,1 are different values. how to only echo 14? like echo $status1; echo $status2; echo $status3; etc..
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.