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.
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
WHEREclause, 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:
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).
making JSON
Since
$dataalready is an array with all the values, you can simply write thisecho json_encode( $data );to generate JSON.making output
Since you have already the array, you can just write:
Any questions ?