Having problems with my COUNT

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

Having problems with my COUNT

Post by bla5e »

Code: Select all

 
       $con = mysql_connect("localhost", "x-", "x") or die("MySQL Error: " . mysql_error());           
        mysql_select_db("x", $con) or die("MySQL Error: " . mysql_error());  
        $countItems = mysql_query("SELECT COUNT * FROM makeMoney WHERE type='CD'");
        $resultItems = mysql_query($countItems) or die(mysql_error());
        
        echo $resultItems;
        mysql_close($con);
 
I am getting an error saying the query is empty.. but the database has rows 8 for CDs

HELP PLEASE :)
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: Having problems with my COUNT

Post by pcoder »

Update your $countItems with

Code: Select all

$countItems = mysql_query("SELECT COUNT(*) FROM makeMoney WHERE type='CD'");
It works 8)
jothirajan
Forum Commoner
Posts: 69
Joined: Tue Jan 27, 2009 12:06 am

Re: Having problems with my COUNT

Post by jothirajan »

Try this tooooooooooooo,...........

# $con = mysql_connect("localhost", "x-", "x") or die("MySQL Error: " . mysql_error());
# mysql_select_db("x", $con) or die("MySQL Error: " . mysql_error());
# $countItems = mysql_query("SELECT * FROM makeMoney WHERE type='CD'");
# $resultItems = mysql_query($countItems) or die(mysql_error());
#
# echo count($resultItems);
# mysql_close($con);
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

Re: Having problems with my COUNT

Post by bla5e »

pcoder wrote:Update your $countItems with

Code: Select all

$countItems = mysql_query("SELECT COUNT(*) FROM makeMoney WHERE type='CD'");
It works 8)
Most Requested Item: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #2' at line 1
whats with this? :(
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Having problems with my COUNT

Post by andyhoneycutt »

you need to use mysql_fetch_xxx (mysql_fetch_assoc, mysql_fetch_row, etc.) in order to get a row from the result. once you have the row you can print_r it or get values from it, e.g.:

Code: Select all

 
$query = "SELECT a, b, c FROM table";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$a = $row['a'];
$b = $row['b'];
$c = $row['c'];
print_r($row);
echo $a, $b, $c;
 
In your example, $countItems is a result resource, not a row.
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

Re: Having problems with my COUNT

Post by bla5e »

andyhoneycutt wrote:you need to use mysql_fetch_xxx (mysql_fetch_assoc, mysql_fetch_row, etc.) in order to get a row from the result. once you have the row you can print_r it or get values from it, e.g.:

Code: Select all

 
$query = "SELECT a, b, c FROM table";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$a = $row['a'];
$b = $row['b'];
$c = $row['c'];
print_r($row);
echo $a, $b, $c;
 
In your example, $countItems is a result resource, not a row.
can u further explain this tome? I feel like there should be an easier way to count the rows without having to set a variable to each one
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Having problems with my COUNT

Post by andyhoneycutt »

Unfortunately, when running a count in a sql statement you will need to take the result and pull a row from it to get the data. counting the number of rows in the result set isn't an option because it will always be 1 for a count(*). Try the following:

Code: Select all

 
$query = "SELECT COUNT(*) as mycount FROM makeMoney WHERE type='CD'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$theCount = $row['mycount'];
echo "the count is: $theCount";
 
-Andy
Post Reply