472,331 Members | 1,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,331 software developers and data experts.

displaying the result of a COUNT query in MySQL

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
12 22665
<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
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
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
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
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
$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
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

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
> ... 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
>
... 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave Posh | last post by:
I seem to be having a problem displaying time stored in mysql. The format stored in the database is 13:15:05. The database data type is time. ...
7
by: Schraalhans Keukenmeester | last post by:
X-Followup: comp.lang.php I have a PHP script that adds messages to a simple MySQL Database. (PHP 5.0.3, MySQL 4.1.1) One of the fields it...
6
by: Nicolae Fieraru | last post by:
Hi All, I have a query, Select Count(BoolField) from tblMyTable, Where BoolField = true. If I run the query by itself, it returns the number...
6
by: dBNovice | last post by:
Hey group, I am trying to do a count of the number of papers in a table. The table has a PaperID that differentiates each paper , e.g. 004.1. ...
0
seshu
by: seshu | last post by:
Hi Every Body This is seshu,today i saw an option in mysqi database in tools menu and the option is export result set ...
4
by: karthikeyanck | last post by:
How to query MySQL from a web browser URL. I 've a Apache server running on my Ubuntu machine which has PHP and MySQL installed. I 've an...
5
by: Harlequin | last post by:
Hi there, I'm something of a newcomer to MS Access so it's more than likely that my question is extremely elementary. I have a form whose...
3
by: printline | last post by:
Hello All I need a little help with a phph script to display some specific data from a mysql table. I have a mysql table with 4 columns and 10...
3
WyvsEyeView
by: WyvsEyeView | last post by:
This seems like it should be so easy to do. I have a table, called tblTopics. Each topic can have one or more instances, contained in a table called...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.