Page 1 of 1

Having problems with my COUNT

Posted: Tue Jan 27, 2009 10:10 pm
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 :)

Re: Having problems with my COUNT

Posted: Tue Jan 27, 2009 11:48 pm
by pcoder
Update your $countItems with

Code: Select all

$countItems = mysql_query("SELECT COUNT(*) FROM makeMoney WHERE type='CD'");
It works 8)

Re: Having problems with my COUNT

Posted: Wed Jan 28, 2009 3:17 am
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);

Re: Having problems with my COUNT

Posted: Wed Jan 28, 2009 10:36 am
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? :(

Re: Having problems with my COUNT

Posted: Wed Jan 28, 2009 11:20 am
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.

Re: Having problems with my COUNT

Posted: Wed Jan 28, 2009 11:34 am
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

Re: Having problems with my COUNT

Posted: Fri Jan 30, 2009 12:01 pm
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