PHP Classes

File: test.php

Recommend this page to a friend!
  Classes of Lasantha Samarakoon   MySQL Database Layer With Dynamic SQL Generators   test.php   Download  
File: test.php
Role: Example script
Content type: text/plain
Description: Test script and the documentation
Class: MySQL Database Layer With Dynamic SQL Generators
Run MySQL database queries from parameter lists
Author: By
Last change:
Date: 15 years ago
Size: 4,760 bytes
 

Contents

Class file image Download
<?php
/******************************************** INSTRUCTIONS TO USE **************************************************************
 * --------------------------------------------------------------------------------------------------------------------------
 * Program : MySQL Database Layer With Dynamic SQL Generators, PHP Class Library
 * Files : database.inc.php, test.php
 * Author : Lasantha Samarakoon
 * Date released : Monday, September 27, 2009
 * Email : lasn1987@gmail.com
 * Licence : http://www.gnu.org/licenses/gpl.txt
 * --------------------------------------------------------------------------------------------------------------------------
 * This program is a freeware, which falls under GNU Genral Public Licence.
 * ---------------------------------------------------------------------------------------------------------------------------
 * You can modify this program, without any permission from the author. But be kind enough to send the updated version to the
 * author through the above mentioned Email address.
 * ---------------------------------------------------------------------------------------------------------------------------
 * Documentation:
 * Please refer the test.php file for hints on the usage of this class library.
 * ---------------------------------------------------------------------------------------------------------------------------
 *
 * ************************************* PROUD TO BE A SRI LANKAN...!!! ******************************************************
 
DOCUMENTATION:

Database class:
    Properties:
        string host
                - database host name
               
        string username
                - database server's username
               
        string password
                - database server's password
               
        string database
                - database name
               
        string connection
                - database connection handler
           
    Methods:
        new Database([string host], [string username], [string password], [string database])
            - constructor
           
        object connect() throws DatabaseException
            - connect to the database server
           
        object use_db([string database]) throws DatabaseException
            - change the working database
           
        resultset query($sql) throws DatabaseException
            - execute an sql query and return the resultset if available
           
        resultset select(string table_name, [array fields], [array criteria]) throws DatabaseException
            - sql select command generator
            - if fields = null, it means select all
           
        object insert(string table_name, array values) throws DatabaseException
            - sql insert command generate
           
        object update(string table_name, array values, [array criteria]) throws DatabaseException
            - sql update command generator
           
        object delete(string table_name, [array criteria]) throws DatabaseException
            - sql delete command generator
           
        object refresh() throws DatabaseException
            - refresh the connection by closing and re-connecting

TEST UNIT:

    Create database:
        Database name: test
        Table: users
            - uid int : not null, primary key, auto increment
            - username varchar(20) : not null
            - password varchar(20) : not null
            - role varchar(10) : not null
           
        Assumptions:
            Database host : localhost
            Database username : root
            Database password : mypass
*/

require_once 'database.inc.php';

// this method displays data by fetching it from the database
function display_data($db) {
   
   
// select username, password from users
   
$res = $db->select('users', array('username', 'password'));
   
   
// traverse through the resultset
   
while($r = mysql_fetch_array($res)) {
       
        echo
"username: " . $r['username'] . "<br />\n";
        echo
"password: " . $r['password'] . "<br />\n";
        echo
"<hr />\n";
    }
   
    echo
"<hr />\n";
}

// create object
$db = new Database('localhost', 'root', 'mypass');

// connect to the database + change the database file
$db->connect()->use_db('test');

// add few data
$db->insert('users', array("username" => "\"user1\"", "password" => "\"pass1\"", "role" => "\"admin\""));
$db->insert('users', array("username" => "\"user2\"", "password" => "\"pass2\"", "role" => "\"manager\""));
$db->insert('users', array("username" => "\"user3\"", "password" => "\"pass3\"", "role" => "\"clerk\""));
$db->insert('users', array("username" => "\"user4\"", "password" => "\"pass4\"", "role" => "\"guest\""));

display_data($db);
// update the table
$db->update('users', array("username" => "\"new_user\""), array("uid = 2"));

display_data($db);

// delete row from the table
$db->delete('users', array("username = \"user3\""));

display_data($db);
?>