Help with mysql table (noob)

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
Foxy999
Forum Commoner
Posts: 45
Joined: Sat Mar 21, 2009 11:50 am

Help with mysql table (noob)

Post by Foxy999 »

How can I return the amount of elements in a table that's in a mysql database?

Foxy
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Help with mysql table (noob)

Post by Christopher »

You can do something like: "SELECT COUNT(*) AS total_records FROM mytable".
(#10850)
Foxy999
Forum Commoner
Posts: 45
Joined: Sat Mar 21, 2009 11:50 am

Re: Help with mysql table (noob)

Post by Foxy999 »

I tried the sql query but I think I am doing something wrong, this is what I have:

$totalQ = "SELECT COUNT(*) AS total_records FROM ELEC";
$totalRecords = mysql_query($totalQ) or die(mysql_error());

But when I echo $totalRecords it prints "Resource id#3"

Please help,
foxy
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Help with mysql table (noob)

Post by Darhazer »

You should not only execute the query, but also fetch the result...

Code: Select all

$totalQ = "SELECT COUNT(*) AS total_records FROM ELEC";
$result = mysql_query($totalQ) or die(mysql_error());
$totalRecords = mysql_fetch_object($result);
echo $totalRecords->total_records
Foxy999
Forum Commoner
Posts: 45
Joined: Sat Mar 21, 2009 11:50 am

Re: Help with mysql table (noob)

Post by Foxy999 »

thanks that worked great
Post Reply