Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8062177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:34:50+00:00 2026-06-05T10:34:50+00:00

This is my actual query in MySQL: SELECT sndprefix, COUNT(*) FROM `inbox` WHERE (

  • 0

This is my actual query in MySQL:

 SELECT sndprefix, COUNT(*) FROM `inbox` WHERE
 (
 sndprefix='22' OR 
 sndprefix='23' OR 
 sndprefix='32' OR 
 sndprefix='33' OR 
 sndprefix='34' OR 
 sndprefix='42' OR 
 sndprefix='43'
 ) 
 GROUP BY sndprefix;

I’m getting the CORRECT response such as:

 sndprefix  COUNT(*)
 22     3
 23     5
 32     1
 33     1
 43     1

My question is what is the PHP code to show this query in the browser and to be parsed as json format too.

Thanks in advance.

Apology if I have not posted the code earlier, I was trying to familiarize myself with stackoverlow’s UI finding the edit option..

Here’s the code I’ve tried so far:

 <?php
 $query="SELECT sndprefix, COUNT(*) FROM `inbox` WHERE
 (
 sndprefix='22' OR 
 sndprefix='23' OR 
 sndprefix='32' OR 
 sndprefix='33' OR 
 sndprefix='34' OR 
 sndprefix='42' OR 
 sndprefix='43'
 ) 
 GROUP BY sndprefix;";

 $result = mysql_query($query);
while($sunget = mysql_fetch_array($result)){
    foreach($sunget AS $key => $value) { $conget[$key] = stripslashes($value); }
 echo "". nl2br( $sunget['sndprefix']) ."";
 }
 ?>

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:34:52+00:00Added an answer on June 5, 2026 at 10:34 am

    Well .. looks like question got updated.

    You are doing it wrong .. at multiple levels. I guess bad tutorials are at fault.

    • The query

      You should read more about WHERE clause, and how to use it. This page will give some basic info, but you should find some book about MySQL (and only about MySQL, no PHP included).

      This would make the query much easier to read:

      SELECT 
          sndprefix AS prefix, 
          COUNT(*) AS count 
      FROM inbox 
      WHERE sndprefix IN ('22', '23', '32', '33', '34', '42', '43') 
      GROUP BY sndprefix;
      

      Also, you might want to learn about JOIN statements in MySQL, because I get a feeling, that this list of specific prefixes has something in common.

      And since the prefixes are repeating, they might be better off in a separate table. At least from normalization point of view.

    • Querying database in PHP

      You should not writing new code with the ancient mysql_* functions. They are no longer maintained and community has begun the deprecation process, and now there is even has been a warning added in manual: (see the red box) .

      Instead you should learn about prepared statements and use either PDO or MySQLi. If you cannot decide, this article will help to choose. If you care to learn, here is a quite good PDO-related tutorial.

      Lets assume that you have use PDO to interact with database (I just prefer it over MySQLi).

      $connection = new PDO(
                        'mysql:host=localhost;dbname=my_magc_db;charset=UTF-8', 
                        'username', 'password');
      $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
      // the connection has been established, and object configured
      
      $sql = 'SELECT 
                  sndprefix AS prefix, 
                  COUNT(*) AS count 
              FROM inbox 
              WHERE sndprefix IN ('22', '23', '32', '33', '34', '42', '43') 
              GROUP BY sndprefix';
      
      $statement = $connection->query( $sql );
      // no need to prepare the statement this time, because values are predefined
      // if values '22', '23', etc. depend on user input, code would be different
      
      $data = $statement->fetchAll(PDO::FETCH_ASSOC);
      // now $data contains all the values.
      // you van use var_dump($data) to "look inside" variable
      
    • making JSON

      Since $data already is an array with all the values, you can simply write this
      echo json_encode( $data ); to generate JSON.

    • making output

      Since you have already the array, you can just write:

      foreach ( $data as $row ) {
          echo $row['prefix'], ' ', $row['count'], PHP_EOL, '<br>';
      }
      

      Any questions ?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using this MySQL query, called by MySQL: SELECT tf_posts.*, tf_threads.thread_id FROM tf_threads LEFT
Consider a tee export of a MySQL query. SELECT * FROM mytable; +----------+-------+----------+-----------+-------+------+--------+ |
This this code: SELECT SUBSTRING(posted,1,4) as year FROM styles reader = cmd1.ExecuteReader(CommandBehavior.CloseConnection); reader.Read(); Response.Write(reader[0].ToString());
I am running the following MySQL query : select * from combinations where family_type='f597';
This is currently my MySQL UPDATE query, which is called from program written in
God damn confused at the syntax for my MYSQL query. Is this correct... Can't
I know in an actual query, you can use auto_increment=1 but how is this
This is the actual error message i'm getting if i run it on an
This is more of a theoretical question than an actual problem I have. If
I keep getting this error and I have tried putting quotes around the actual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.