Log stats
Welcome to the second part of this mini tutorial / code snippet . Now we will log some info to our MySQL database using PHP .
Here is the script we are using , you will have to input ypour own host , username and password for this to operate correctly.
<?php
//store the users browser in a variable
$users_browser = $HTTP_USER_AGENT;
//store the users IP in a variable
$users_ip = $REMOTE_ADDR;
//database connection
$connection = mysql_connect("localhost" , "username" , "password");
//select the stats database
mysql_select_db("stats" , $connection);
//insert the info into the stats table
$sql_query = "INSERT INTO stats(browser,ip,logdate)
VALUES('$users_browser','$users_ip',now())";
//store
$result = mysql_query($sql_query);
?>
Now all we have to do is test the script works correctly , save the above and call it something like logstats.php , now load this page into your web browser , if you have more than one different make of browser that is even better .
Now go back to MySQL command line and type in the following
SELECT * FROM stats;
If all has gone to plan you should now see an entry in your table , here is a screen capture of what we had displayed.

Note this is only part of the table that was displayed but as you can see it has logged 2 different browsers Internet Explorer 5.5 and Opera 5 as well.
In the next part we will show how to display the results in an HTML table in your browser.
Back to Part 1 , Forward to Part 3
|