Forum Moderators: coopster

Message Too Old, No Replies

Basic SELECT COUNT query

Find out how many rows there are in my database.

         

ski442

10:59 pm on May 14, 2008 (gmt 0)

10+ Year Member



Hi Guys.

This should be easy but i can not get to work, bearing in mind i'm still learning.

What id like to have the number of products or rows in my db displayed in certain catagorys on my site, but stuck at the first hurdle of trying to understand the code and get it to work.

I think this may be wrong. I have searched this site for more cluse but can't see anything.

<?php
virtual('/include.php');

$sql = "SELECT COUNT (*) FROM products ";
$row = mysql_fetch_array($result);
{
echo $result[0];
}
?>

I have no error message or nothing, the whole page renders as it should. I have also played around with code by changing parts but no good.

Your help as always will be great.
Many thanks in advance
Ski442

ag_47

3:56 am on May 15, 2008 (gmt 0)

10+ Year Member



I'm not sure what exactly you're trying to achieve, but if you want to read up on php/mysql, I'd suggest this:
[w3schools.com...]

mrscruff

11:31 am on May 15, 2008 (gmt 0)

10+ Year Member



This should work:

<?php
virtual('/include.php');

//need to have connected using mysql_connect()
//before using mysql_query()

$sql = "SELECT COUNT (*) FROM products ";
$result = mysql_query($sql);
if ($result !== false) {
$row = mysql_result($result, 0);
echo $row;
} else {
//sql error
}

?>

omoutop

12:04 pm on May 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



this is also usefull:

$query = "SELECT [some_fields] FROM [tablename] WHERE [criteria]";
$result = mysql_query($query);
$total_rows = mysql_num_rows($result);