Page 1 of 1

Help with mysql table (noob)

Posted: Sun Jul 12, 2009 6:38 pm
by Foxy999
How can I return the amount of elements in a table that's in a mysql database?

Foxy

Re: Help with mysql table (noob)

Posted: Sun Jul 12, 2009 11:19 pm
by Christopher
You can do something like: "SELECT COUNT(*) AS total_records FROM mytable".

Re: Help with mysql table (noob)

Posted: Mon Jul 13, 2009 3:57 pm
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

Re: Help with mysql table (noob)

Posted: Mon Jul 13, 2009 4:13 pm
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

Re: Help with mysql table (noob)

Posted: Mon Jul 13, 2009 6:29 pm
by Foxy999
thanks that worked great