Lokking through db and adding numbers

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

Lokking through db and adding numbers

Post 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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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?
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

Post 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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

mysql_fetch_assoc requires the resource id returned by mysql_query.

See http://us.php.net/manual/sv/function.my ... -assoc.php
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

Post 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]
Post Reply