473,387 Members | 1,493 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,387 software developers and data experts.

MySql count

The following snippet (for whatever reason) returns no value for the
count. Suggestions?
$arr = array ("A", "B", "C", "D", "E");
foreach ($arr as $client) {
$count = mysql_query('SELECT COUNT(*) from table where columnA =
$client');
echo "$client has $count";
echo '<br>';
}

Nov 20 '06 #1
9 2718
Akhenaten wrote:
The following snippet (for whatever reason) returns no value for the
count. Suggestions?
$arr = array ("A", "B", "C", "D", "E");
foreach ($arr as $client) {
$count = mysql_query('SELECT COUNT(*) from table where columnA =
$client');
echo "$client has $count";
echo '<br>';
}

1. You can't pass variables within single quotes.

Instead of '... $client' you either need "... $client" or '... '.$client

2. Strings in MySQL queries need to be surrounded by quotes (either single or
double).

This will work:

"SELECT COUNT(*) from table where columnA = '$client'"

as will this:

'SELECT COUNT(*) from table where columnA = "'.$client.'"'

--
Christoph Burschka
Nov 20 '06 #2
..oO(Christoph Burschka)
>Akhenaten wrote:
>The following snippet (for whatever reason) returns no value for the
count. Suggestions?
[...]

1. You can't pass variables within single quotes.
[...]

2. Strings in MySQL queries need to be surrounded by quotes (either single or
double).
[...]
3. mysql_query() just returns a resource ID. You have to use one of the
mysql_fetch_* functions to get the actual results.

Micha
Nov 20 '06 #3
On Mon, 20 Nov 2006 18:48:39 +0100, Michael Fesser <ne*****@gmx.dewrote:
>.oO(Christoph Burschka)
>>Akhenaten wrote:
>>The following snippet (for whatever reason) returns no value for the
count. Suggestions?
[...]

1. You can't pass variables within single quotes.
[...]

2. Strings in MySQL queries need to be surrounded by quotes (either single or
double).
[...]

3. mysql_query() just returns a resource ID. You have to use one of the
mysql_fetch_* functions to get the actual results.
4. Always check for errors, as the database can tell you what went wrong via
mysql_error().

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Nov 20 '06 #4
Actually fixed it using the following:

$arr = array ("A", "B", "C", "D", "E");
foreach ($arr as $client) {
$query = mysql_query("SELECT * from table where columnA = '$client' ");
$num_rows = mysql_num_rows($query);
echo "$client has $num_rows";
echo '<br>';
Unsure as why but for whatever reason I simply can't get a value using
count <pounding head on keyboard>.

On Nov 20, 11:32 am, Christoph Burschka
<christoph.bursc...@rwth-aachen.dewrote:
Akhenaten wrote:
The following snippet (for whatever reason) returns no value for the
count. Suggestions?
$arr = array ("A", "B", "C", "D", "E");
foreach ($arr as $client) {
$count = mysql_query('SELECT COUNT(*) from table where columnA =
$client');
echo "$client has $count";
echo '<br>';
}1. You can't pass variables within single quotes.

Instead of '... $client' you either need "... $client" or '... '.$client

2. Strings in MySQL queries need to be surrounded by quotes (either single or
double).

This will work:

"SELECT COUNT(*) from table where columnA = '$client'"

as will this:

'SELECT COUNT(*) from table where columnA = "'.$client.'"'

--
Christoph Burschka
Nov 20 '06 #5
Akhenaten wrote:
The following snippet (for whatever reason) returns no value for the
count. Suggestions?
$arr = array ("A", "B", "C", "D", "E");
foreach ($arr as $client) {
$count = mysql_query('SELECT COUNT(*) from table where columnA =
$client');
echo "$client has $count";
echo '<br>';
}
I see no single-quotes around $client in the SELECT
query.
That would make "$client" seem to be a field or
variable name, instead of a string value.

That probably means you're querying for where
columnA = the value of a variable named "$client"
instead of what you meant to query for which was
where columnA = the string in "$client".

I do that so much - it's one of the first errors I
check for anymore!
Nov 20 '06 #6
..oO(Akhenaten)
>Actually fixed it using the following:

$arr = array ("A", "B", "C", "D", "E");
foreach ($arr as $client) {
$query = mysql_query("SELECT * from table where columnA = '$client' ");
$num_rows = mysql_num_rows($query);
echo "$client has $num_rows";
echo '<br>';
Unsure as why but for whatever reason I simply can't get a value using
count <pounding head on keyboard>.
As said, it requires a mysql_fetch_* function to get the results from a
query. Your "fix" above is just an ugly hack. Additionally with some
more SQL and a GROUP BY clause you could drop the foreach loop and do it
all with a single query, something like

SELECT columnA, COUNT(*) AS count
FROM table
WHERE columnA IN ('A', 'B', 'C', 'D', 'E')
GROUP BY columnA
ORDER BY columnA

Micha
Nov 20 '06 #7
Andy Hassall wrote:
On Mon, 20 Nov 2006 18:48:39 +0100, Michael Fesser <ne*****@gmx.dewrote:
>>.oO(Christoph Burschka)
>>>Akhenaten wrote:
The following snippet (for whatever reason) returns no value for the
count. Suggestions?
[...]
code re-inserted
>>>$arr = array ("A", "B", "C", "D", "E");
foreach ($arr as $client) {
$count = mysql_query('SELECT COUNT(*) from table where columnA =
$client');
echo "$client has $count";
echo '<br>';
}
>>1. You can't pass variables within single quotes.

2. Strings in MySQL queries need to be surrounded by quotes

3. mysql_query() just returns a resource ID.

4. Always check for errors,
5. Indent your code. Always. Even for a very small example posted to
usenet.

--
I (almost) never check the dodgeit address.
If you *really* need to mail me, use the address in the Reply-To
header with a message in *plain* *text* *without* *attachments*.
Nov 20 '06 #8
Michael Fesser wrote:
.oO(Akhenaten)
>Actually fixed it using the following:

$arr = array ("A", "B", "C", "D", "E");
foreach ($arr as $client) {
$query = mysql_query("SELECT * from table where columnA = '$client' ");
$num_rows = mysql_num_rows($query);
echo "$client has $num_rows";
echo '<br>';
Unsure as why but for whatever reason I simply can't get a value using
count <pounding head on keyboard>.

As said, it requires a mysql_fetch_* function to get the results from a
query. Your "fix" above is just an ugly hack. Additionally with some
more SQL and a GROUP BY clause you could drop the foreach loop and do it
all with a single query, something like

SELECT columnA, COUNT(*) AS count
FROM table
WHERE columnA IN ('A', 'B', 'C', 'D', 'E')
GROUP BY columnA
ORDER BY columnA

Micha
And since one of the bottle-necks in a web application is the time it takes for
the database to process the query and return the result set, it makes sense to
minimize the number of queries. The above is the best way to go. Use it in this
code:

$sql = "...[shown above]";
$res = mysql_query($sql);
$counts=array();
while ($row=mysql_fetch_array($res)) $counts[$row['columnA']]=$row['count'];

in the end, $counts will be an array containing all the letters as keys and the
associated counts as values.

--
Christoph Burschka
Nov 20 '06 #9
I created a function get_query_rows() that makes the repetitive task of
using mysql_query() and looping through abnd getting the results easier
(for me anyhow).
$arr = array ("A", "B", "C", "D", "E");
foreach ($arr as $client)
{
$query = 'SELECT COUNT(*) as count from table where columnA =
"'.$client.'"';
$rows = get_query_rows($query);
$count = $rows[0]['count'];
echo "$client has $count<BR />";
}

function get_query_rows($query,$resource=null)
{
$rows = false;
$result = ( $resource )
? @mysql_query($query,$resource)
: @mysql_query($query);
if ( $result )
{
$rows = array();
$num_rows = mysql_num_rows($result);
for ( $i=0; $i<$num_rows; $i++ )
$rows[] = mysql_fetch_assoc($result);
}
else
trigger_error(mysql_error());
return $rows;
}

Nov 20 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Fatt Shin | last post by:
Hi, I'm running MySQL 4.0.13, connecting from PowerBuilder 9 using ODCB Connector 3.51. I'm facing a problem where whenever I issue a SELECT COUNT(*) statement from PowerBuilder, I always get SQL...
0
by: Philip Stoev | last post by:
Hi all, Please tell me if any of this makes sense. Any pointers to relevant projects/articles will be much appreciated. Philip Stoev http://www.stoev.org/pivot/manifest.htm ...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
4
by: Ross Contino | last post by:
Hello to all: I have been searching the web for examples on how to determine a median value in a mySQL table. I have reviewed the article at...
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...
1
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
3
by: auron | last post by:
Hi there, I have a really stupid and banal problem with showing the results of a MySQL query in PHP, preciselly with MySQL count() function that gives to a variable in PHP the result. NOTE:...
6
by: ojorus | last post by:
Hi! My company make several flash-based games, and I use php to communicate with mysql to provide highscore-lists. My problem is this: When I save a player's score in the mysql-table, I want to...
1
by: Ike | last post by:
Recently, I began using a different MySQL verver (i.e. different machine as well as different version#, going from 4.12a to 4.1.9 max). The following query used to work: select firstname,...
3
by: Auddog | last post by:
I have the following query that works in mysql: select id, order_no, price, count(item_no), sum(price) from production WHERE item_no = '27714' group by item_no; When I setup my query in php,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.