0

* edit * I asked this question as a php question, without the tag of mySql (which has been added by another SO member --> not a big deal to me, but just saying). I am sorry for any confusion the inclusion of the ?extraneous? detail that the array was from a mySql query!

I am hoping to keep the queried data from the SELECT * to loop through using PHP in an array. I thought, in order to keep this code simple, that PHP could count the number of times a string occurs in a particular position in an array. * /edit *

I would like to find out how many times a string appears in a particular column from a mySql query result.

For example, lets say I have a table 'automobiles' and their is a column 'type' in that table. The column 'type' has values such as 'sedan', 'van', 'suv' etc.

If I query everything from the table like this:

$query = "SELECT *
            FROM automobiles";

and then insert the queried values into an array:

$the_array = mysql_fetch_array($query);

and then try to quantify the number of times a string occurs like this:

$count = array_count_values($the_array);
print_r($count);

not only does this not work, but the data would be inaccurate because the values would be based on the entire table, not on the automobile 'type' column alone.

4
  • Hmmm, downvotes for asking easy question? Commented Jul 3, 2011 at 18:13
  • @superUntitled Eh, I'd say it belongs here. It's fairly basic, but everyone has to start somewhere... Commented Jul 3, 2011 at 18:13
  • Thanks everyone for your help. I think that I do understand the capabilities of the mySql COUNT() function, as well as the php funtion php_num_rows(). I would like to use all the data in the queried table (hence the 'SELECT *'), as I would like to echo back that data in a while(){} loop. I would think that php would be able to count the number of times a string appears in a particular array position. Commented Jul 3, 2011 at 18:16
  • whoops, i seem to have asked a question that is outside of the scope of the stack overflow community. How is this question not programming related? It would be nice for downvoters to let me know, so I can better serve this community in the future. Commented Jul 3, 2011 at 18:17

3 Answers 3

5
$query = "SELECT `type`, COUNT(*) `count` FROM `automobiles` GROUP BY `type`";
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your insite ceejayoz! however, this will only return the COUNTed values, not the rest of the data in the table. My question pertained to the counting of a string within a php array.
1

You can do this with a simple SQL aggregation:

SELECT type, COUNT(*) FROM automobiles GROUP BY type

Comments

0

or you can go

$query = ("SELECT * FROM automobiles WHERE type='TYPE'");
$total_count = mysql_num_rows($query);
echo $total_count;

4 Comments

Not much fun when you have a couple dozen (or million) types.
then set a variable to only look at one at a time.
Regarding your now-deleted comment, that wasn't my "childish" downvote.
i didnt say it was yours as when i posted my comment yours was not there. sorry to make it feel like it was you. my bad.

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.