PHP database logging

 In PHP, Programming

In order to make database logging in php we should first create a table with 2 fields: the message that we log and the date that the log was made. After creating the database we should create a php class that will have to pass the instance of the MySQL database to the log class contructor. After that as previous example of php log file we should call a function that inserts a new row in a table database. Here is the class for php database logging:

class DBLog {
private $_db;

public function __construct($db) {
$this->_db = $db;

public function WriteLogToDB($message) {
$date = date("Y-m-d h:m:s");
mysql_query ("INSERT INTO DBLog VALUES('".$message."', '$date')", $this->_db);

In order to call the function of database logging we should first make a connection to the database and then call the function to save row log to database:

$connection = mysql_connect("localhost", "username", "password")
or die("Unable to connect to MySQL server");
mysql_select_db('DatabaseName', $connection) or die ("Database not found.");
$filel = new DBLog($connection);
$filel->WriteLogToDB('Here is my first log.');

We only need the mysql script that creates the table in our database:

`Message` text NOT NULL,
`DateCreated` datetime NOT NULL

Recent Posts
Contact Us

We're not around right now. But you can send us an email and we'll get back to you, asap.

Not readable? Change text. captcha txt

Start typing and press Enter to search