so I have this query:
SELECT COUNT( * ) AS cnt
FROM table
WHERE datetime > NOW( ) - INTERVAL 45
SECOND
When I preform this query in MySQL I get the result: cnt 25
(http://puu.sh/7ZNh.png)
I now want to echo this in my PHP page, how would I do this?
UPDATE:
Here is the full code
<?php
$con = mysql_connect("host","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$query = ("SELECT COUNT(*) FROM usersonline WHERE datetime > NOW() - INTERVAL 5 MINUTE");
$result = mysql_query($query);
?>
list($count) = @mysql_fetch_row(mysql_query($sql));
echo $count;
Within your PHP application you need to setup a database object which can be used to connect to your database in order to execute your queries. A good place to start is with PHP PDO, more info @ http://us.php.net/manual/en/pdo.query.php along with some good examples.
$con = mysql_connect("host","user","pass");
if (!$con)
{
die('Could not connect: '.mysql_error());
}
mysql_select_db("db", $con);
$query = ("SELECT COUNT(*) FROM usersonline
WHERE datetime > DATESUB(NOW(),INTERVAL 5 MINUTE) ");
$result = mysql_query($query);
if (!$result)
{
die('Error in select statement: '.mysql_error());
}
$row = mysql_fetch_array($result);
echo "number of rows = ".htmlentities($row['cnt']);