PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Marian VASILE   SQL Engine   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: example - using SQLEngine
Class: SQL Engine
Execute common database access SQL queries
Author: By
Last change:
Date: 18 years ago
Size: 3,030 bytes
 

Contents

Class file image Download
<?php

// An example of how to use SQLEngine; requires PEAR, PEAR::DB
// Creates a table `members`, updates two records and display a bit of a greeting
// This example is not created to look good (although it's pretty fine i think), but to demonstrate simple functionality.
// To get log functionality please issue a `chmod 0777 log` to allow apache to write on it


session_start();
$_include_path = "./";
require_once(
$_include_path."dbcontext/dbconnect.inc.php");
require_once(
$_include_path."dbcontext/Logger.class.php");
require_once(
$_include_path."dbcontext/SQLEngine.class.php");

function
init_app() {
    global
$sql_engine, $k__author, $db_insert_id;
   
$sql_engine = new SQLEngine();
   
// create a table to play with, if not existing already, and add a record.
    // SQLEngine will automatically create rec_id column, and some other info, as well as history table.
   
$sql_engine->check_create_table("members", "username:VARCHAR(255):NOT NULL,fullname:VARCHAR(255):NULL,email:VARCHAR(255):NULL,password:VARCHAR(255):NOT NULL,role:VARCHAR(32):NULL");
   
// check to see if we have a member inserted:
   
$res = $sql_engine->exec_query("SELECT rec_id FROM members ORDER BY rec_id DESC");
    if ( !
$row = $sql_engine->next($res) ) {
       
$res = $sql_engine->exec_query("INSERT INTO members (username, fullname, password) VALUES ('me', 'me again', 'me too as hashed password')");
       
$_SESSION['member_id'] = $db_insert_id; // just for testing now...
   
}
    else
$_SESSION['member_id'] = $row['rec_id'];
}



global
$sql_engine, $k__author, $db_insert_id, $debug;
$debug = true;
error_reporting(E_ALL);
init_app();

// if session managed login, then get the current user as author of all sql operations
// this author will be recorded in the data history log
if (!isset($_SESSION['member_id'])) $_SESSION['member_id'] = 1;
$current_userid = $_SESSION['member_id'];
$k__author = $current_userid;

$res = $sql_engine->exec_query("SELECT * FROM members WHERE rec_id=$_SESSION[member_id]");
$row = $sql_engine->next($res);
echo
"Hello dear $row[fullname] (<strong>$row[username]</strong>)<BR/><hr/>";

$sql_engine->exec_query("INSERT INTO members (fullname, email, username, password, role) VALUES ('Cute Bite ".rand(0, 999)."', '[email protected]', 'cutebite', 'hashed passwd', 'arbiter')");

echo
"New user is now recorded in both active and history tables. Active table rec_id for this user: $db_insert_id <BR/><hr/>";

// If you check your tables you'll see now that a rec_id has been automatically generated upon INSERT. On Oracle as well.

$sql_engine->exec_query("UPDATE members SET email='me".rand(0,999)."@fileleader.com' WHERE rec_id=$_SESSION[member_id]");

// If you check your tables you'll see now that a new history data was updated to record the change.

$sql_engine->exec_query("DELETE FROM members WHERE rec_id=$_SESSION[member_id]");

// If you check your tables you'll see now that a new history data was updated to record this deletion.

echo "Here's a list of changes made to our data:<BR>";

require(
"datahistory.php");

?>