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: 7 years ago
Size: 4,760 bytes


Class file image Download
/******************************************** INSTRUCTIONS TO USE **************************************************************
 * --------------------------------------------------------------------------------------------------------------------------
 * Program : MySQL Database Layer With Dynamic SQL Generators, PHP Class Library
 * Files :, test.php
 * Author : Lasantha Samarakoon
 * Date released : Monday, September 27, 2009
 * Email :
 * Licence :
 * --------------------------------------------------------------------------------------------------------------------------
 * 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...!!! ******************************************************

Database class:
        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
        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


    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
            Database host : localhost
            Database username : root
            Database password : mypass

require_once '';

// 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)) {
"username: " . $r['username'] . "<br />\n";
"password: " . $r['password'] . "<br />\n";
"<hr />\n";
"<hr />\n";

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

// connect to the database + change the database file

// 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\""));

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


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