PHP/MySQL Database Class

This is a simple and easy database class to use in a PHP/MySQL application. I have used Object Oriented Programing (OOP) methods to made this very useful in OOP applications.

class.database.php

<?php
// database class
class database {
    protected static $connection;
    
    var $hostname="localhost";
    var $username="root";
    var $password="";
    var $database="test_database";

    public function __construct() {
        
    }

    public function connect() {    
        if(!isset(self::$connection)) {               
            self::$connection = new mysqli($this->hostname,$this->username,$this->password,$this->database);
        }

        // If connection was not successful, handle the error
        if(self::$connection === false) {
            // Handle error - notify administrator, log to a file, show an error screen, etc.
            return false;
        }

        self::$connection -> query("SET NAMES 'utf8'");

        return self::$connection;
    }


    public function query($query) {
        // Connect to the database
        $connection = $this -> connect();
        // Query the database
        $result = $connection -> query($query);

        return $result;
    }

    public function multi_query($query){
        // Connect to the database
        $connection = $this -> connect();

        // Query the database
        $result = $connection -> multi_query($query);

        return $result;
    }

    public function insert($query) {
        // Connect to the database
        $connection = $this -> connect();

        // Query the database
        $connection -> query($query);
        // Get inserted id
        $insertid = $connection -> insert_id;

        return $insertid;
    }

    public function select($query) {
        $rows = array();
        $result = $this -> query($query);
        if($result === false) {
            return false;
        }
        while ($row = $result -> fetch_assoc()) {
            $rows[] = $row;
        }
        return $rows;
    }

    public function num_rows($query) {
        $result = $this -> query($query);
    
        if($result === false) {
            $count = 0;
        }
        else $count = $result->num_rows;
        
        return $count;
    }

    /**
     * Fetch the last error from the database
     * 
     * @return string Database error message
     */
    public function error() {
        return self::$connection -> error;
    }

    /**
     * Quote and escape value for use in a database query
     *
     * @param string $value The value to be quoted and escaped
     * @return string The quoted and escaped string
     */
    public function escape($value) {
        $connection = $this -> connect();
        return $connection -> real_escape_string(trim($value));
    }
}

$database = new database();
?>

How to use this in a PHP application ?

You must include this in your php page where you need to get data from mysql database or write data to mysql database.

Let’s assume, we have system_users table in our test database. Now we want to get users from this table. So, here is my get_users.php file.

<?php
require('class.database.php'); 

$sql = "SELECT `user_name`, `user_email`, `user_password` FROM `system_users` WHERE 1";
$users = $database->select($sql);

echo json_encode($users);
?>

Download source code from my Github: https://github.com/lahirutm/PHP-MySQL-Database-Class