Log Stats in PHP
Now for the final part we will simply display the results
of the stats table on the page . Here is the script
, remember you will have to modify the username and
password values to your own values. In this example
we will show only 10 entries maximum , you can change
this by altering or removing the LIMIT 10;
<?php
//make a connection
$connection = mysql_connect("localhost","username","password");
//select the database
mysql_select_db("stats" , $connection);
//create a sql query select all from stats table
$sql_query = "SELECT * FROM stats LIMIT 10";
//store query results in the $results variable
$results = mysql_query($sql_query);
//start our table
echo ("<table width ='100%' border ='1'>");
//this is simply the headings
echo ("<tr><th>id</th><th>browser</th><th>I.P
address</th><th>Date of visit</th></tr>");
//loop through the rows
while ($tablerows = mysql_fetch_row($results))
{
echo("<tr>");
//this displays the ID field
echo("<td>$tablerows[0]</td>");
//this displays the browser field
echo("<td>$tablerows[1]</td>");
//this displays the ip field
echo("<td>$tablerows[2]</td>");
//this displays the logdate field
echo("<td>$tablerows[3]</td>");
echo("</tr>");
}
echo ("</table>");
?>
In a future tutorial we will add more stats , produce
percentage shares for browsers , Operating Systems and
also display daily hits , weekly hits , monthly hits
and yearly hits.
|