473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 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 22760
<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. I'm using asp vbscript and sql to retrieve the 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 stores is msgid. The new msgid is a count of all...
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 of true records I want to use the result of that...
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. Some papers are reused. The reused paper is given...
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 when i click on that it is asking me to...
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 assignment to demonstrate how SQL Injection works, I...
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 record source is a single table in my database. It's...
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 rows. I want to display fx. data from row 4, 6, 8...
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 tblTopicInst. tblTopics is bound to a form called...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.