By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,650 Members | 753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,650 IT Pros & Developers. It's quick & easy.

displaying the result of a COUNT query in MySQL

P: n/a
Does anyone have a good approach to displaying in PHP a simple COUNT
query that is performed on a table in a MySQL db?

Thanks

Dec 19 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
<br**********@gmail.com> kirjoitti
viestissä:11**********************@g44g2000cwa.goo glegroups.com...
Does anyone have a good approach to displaying in PHP a simple COUNT
query that is performed on a table in a MySQL db?

$result = mysql_query('SELECT COUNT(*) AS foo FROM table') or
die(mysql_error());
$bar = mysql_fetch_array($result);
echo $bar['foo'];

--
SETI @ Home - Donate your cpu's idle time to science.
Further reading at <http://setiweb.ssl.berkeley.edu/>
Kimmo Laine <an*******************@gmail.com.NOSPAM.invalid>
Dec 19 '05 #2

P: n/a
my code:

$query6 = "SELECT COUNT (movie_id) as quant FROM movies WHERE divx = 1
AND format = 'reg'";
$result6 = mysql_fetch_array($query6);

later on the page:

echo $result6[quant]

result:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
result resource in /home/tgupc/public_html/admin/divxreport.php on line
19
Any suggestions?

thanks

Dec 19 '05 #3

P: n/a
On 19 Dec 2005 11:50:43 -0800, br**********@gmail.com wrote:
my code:

$query6 = "SELECT COUNT (movie_id) as quant FROM movies WHERE divx = 1
AND format = 'reg'";
$result6 = mysql_fetch_array($query6);

later on the page:

echo $result6[quant]

result:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
result resource in /home/tgupc/public_html/admin/divxreport.php on line
19


Kimmo posted code with basic error handling, which you've removed. Put it back
in again and it'll tell you why it failed.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Dec 19 '05 #4

P: n/a
I put it back in, and the exact same result occurs.

FYI, line 19 is as follows:

$result6 = mysql_fetch_array($query6);

Dec 19 '05 #5

P: n/a
On 19 Dec 2005 12:01:34 -0800, br**********@gmail.com wrote:

Please quote some context when replying. The default "reply" at the bottom in
Google Groups cuts out all previous text; this is not the accepted way to post
to Usenet. Click "show options" next to the author/date for the message, then
use the "Reply" option there; this quotes and attributes the previous message.
Then follow:

http://groups.google.com/googlegroup...html#summarize

... i.e. don't just quote the lot, unless the message is small and the whole
message is relevant to your reply.
I put it back in, and the exact same result occurs.

FYI, line 19 is as follows:

$result6 = mysql_fetch_array($query6);


It can't have got here and produced the same error, if you put in the error
handling Kimmo posted.

Post your revised code for the lines between mysql_query and
mysql_fetch_array.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Dec 19 '05 #6

P: n/a
$query6 = "SELECT COUNT (movie_id) as quant FROM movies WHERE divx = 1
AND format = 'reg'" or die(mysql_error());
$result6 = mysql_fetch_array($query6);

later on the page:

echo $result6[quant]

result:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
result resource in /home/tgupc/public_html/admin/divxreport.php on line
19
see for yourself: http://www.tgupc.com/admin/divxreport.php

Andy Hassall wrote:
On 19 Dec 2005 12:01:34 -0800, br**********@gmail.com wrote:
It can't have got here and produced the same error, if you put in the error
handling Kimmo posted.

Post your revised code for the lines between mysql_query and
mysql_fetch_array.


Dec 19 '05 #7

P: n/a
On 19 Dec 2005 12:24:35 -0800, br**********@gmail.com wrote:

Kimmo originally wrote:
$result = mysql_query('SELECT COUNT(*) AS foo FROM table') or
die(mysql_error());

But you've used:
$query6 = "SELECT COUNT (movie_id) as quant FROM movies WHERE divx = 1
AND format = 'reg'" or die(mysql_error());
You are missing the call to mysql_query().
$result6 = mysql_fetch_array($query6);
... so $query6 just contains the SQL string, and not a MySQL result set
resource identifier. You're not actually executing the SQL anywhere, or if you
are in the rest of the code, you haven't done the error checking there.
later on the page:

echo $result6[quant]


This should be: $result6['quant'].

See:

http://www.php.net/manual/en/languag...es.array.donts

p.s. You've got the posting style nearly right - however you should put your
new message _under_ the old one, not above, so the whole message makes sense
read on its own. You have "top posted".

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Dec 19 '05 #8

P: n/a

Andy Hassall wrote:
On 19 Dec 2005 12:24:35 -0800, br**********@gmail.com wrote:

Kimmo originally wrote:
$result = mysql_query('SELECT COUNT(*) AS foo FROM table') or
die(mysql_error());


But you've used:
$query6 = "SELECT COUNT (movie_id) as quant FROM movies WHERE divx = 1
AND format = 'reg'" or die(mysql_error());


You are missing the call to mysql_query().
$result6 = mysql_fetch_array($query6);


... so $query6 just contains the SQL string, and not a MySQL result set
resource identifier. You're not actually executing the SQL anywhere, or if you
are in the rest of the code, you haven't done the error checking there.
later on the page:

echo $result6[quant]


This should be: $result6['quant'].

See:

http://www.php.net/manual/en/languag...es.array.donts

p.s. You've got the posting style nearly right - however you should put your
new message _under_ the old one, not above, so the whole message makes sense
read on its own. You have "top posted".

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool


Dec 19 '05 #9

P: n/a
> ... so $query6 just contains the SQL string, and not a MySQL result set
resource identifier. You're not actually executing the SQL anywhere, or if you
are in the rest of the code, you haven't done the error checking there.
I have this part working now, thanks to your help.
later on the page:

echo $result6[quant]


This should be: $result6['quant'].

See:

http://www.php.net/manual/en/languag...es.array.donts


So if I am echoing the result in between strings of HTML, I am having a
hard time knowing / remembering how to "escape" the single-quote marks
inside the result brackets. Does that make sense?

Thanks

Brian

Dec 19 '05 #10

P: n/a
>
... so $query6 just contains the SQL string, and not a MySQL result set
resource identifier. You're not actually executing the SQL anywhere, or if you
are in the rest of the code, you haven't done the error checking there.


I have this part working now. Thanks for your help.
later on the page:

echo $result6[quant]


This should be: $result6['quant'].

See:

http://www.php.net/manual/en/languag...es.array.donts


I have tried to figure out how to single-quote the contents of the []
but when it is concatenated in between strings of HTML, it goofs things
up. I cannot remember how to do that. I tried a few things I noticed in
the page you link to above, but I am not getting very far.

Dec 19 '05 #11

P: n/a
On 19 Dec 2005 14:08:15 -0800, br**********@gmail.com wrote:
This should be: $result6['quant'].

See:

http://www.php.net/manual/en/languag...es.array.donts


I have tried to figure out how to single-quote the contents of the []
but when it is concatenated in between strings of HTML, it goofs things
up. I cannot remember how to do that. I tried a few things I noticed in
the page you link to above, but I am not getting very far.


Use the "curly brace" format within a string:

echo "{$result6['quant']}";
http://www.php.net/manual/en/languag...arsing.complex

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Dec 19 '05 #12

P: n/a
briansmcc try your SQL like this...

$result6 = mysql_query('SELECT COUNT (movie_id) as quant FROM movies
WHERE divx = 1 AND format = 'reg') or die(mysql_error());
$movie_count = mysql_fetch_array($result6 );

later on the page:
echo $movie_count['quant'];

Dec 20 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.