Page 1 of 1

Lokking through db and adding numbers

Posted: Wed Apr 04, 2007 3:28 am
by jmansa
If i have a db with alot of records "numbers" and I want to add how many times the number "1" comes depending on a ID... How do I do that?

Posted: Wed Apr 04, 2007 3:37 am
by Benjamin

Code: Select all

SELECT count(*) AS `total` FROM `table_name` WHERE `field`='1' AND `ID`='5';

Code: Select all

$x = mysql_fetch_assoc();
$total = $x['total'];
Is that what you mean?

Posted: Wed Apr 04, 2007 4:01 am
by jmansa
I tryid it like this:

Code: Select all

$query="SELECT count(*) AS total FROM oak_placement WHERE place='1' AND name='David Hanson'";
$result=mysql_query($query) or die("Unable to find requested user");

$currUser = mysql_fetch_assoc(); 
$total = $currUser['total']; 

echo 'David hanson has com in 1'st place ' .$currUser['total']. ' times.';
But I get this error:
Warning: Wrong parameter count for mysql_fetch_assoc()
Where is the error?

Posted: Wed Apr 04, 2007 4:08 am
by Benjamin
mysql_fetch_assoc requires the resource id returned by mysql_query.

See http://us.php.net/manual/sv/function.my ... -assoc.php

Posted: Wed Apr 04, 2007 4:36 am
by jmansa
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Solved it... Thanks alot for your input!

Code: Select all

$result=mysql_query($query) or die("Unable to find requested user");

$currUser = mysql_fetch_array( $result ); 
$total = $currUser['total'];

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]