I have this PHP code of:
$count = "select
count(fruit) as total,
sum(fruit like '%apple%') as apple,
sum(fruit like '%orange%') as orange
FROM my_wonderful_table_of_fruits";
$count = mysql_query($count);
$count = mysql_fetch_row($count);
I’m trying to store these in a variable and can’t seem to catch them :/
My code is:
while ($row = mysql_fetch_array($count)) {
$count_total = $row['total'];
$count_apple = $row['apple'];
$count_orange = $row['orange'];
}
And I was expecting to be able to echo them like so:
echo "$count_total[0] is the total of $count_apple apples and $count_orange oranges
When I run this query in MySQL Admin, I get a nice row that looks like:
total apple orange
5 3 2
Anyone know how what I’m doing wrong? (besides the fact that I’m using the ‘evil’ version of mysql_fetch_row)
Much appreciated!
You need to reanalyse what you’re doing. Your SQL will only return one row with some aggregate results, but it seems you expect to be able to iterate over several rows of results (due to your
while()).What your sql does and how you’re trying to use it are two different things.