PHP Classes

File: class_database_examples.php

Recommend this page to a friend!
  Classes of MARY Matthieu   Class Database   class_database_examples.php   Download  
File: class_database_examples.php
Role: Example script
Content type: text/plain
Description: example of use class database in your class
Class: Class Database
PEAR DB wrapper with auto-increment id tracking
Author: By
Last change:
Date: 20 years ago
Size: 2,706 bytes
 

Contents

Class file image Download
<?php
require_once 'class_database.php';

$db_connection = array(
    
'type' => 'mysql',
    
'login' => 'root',
    
'mdp' => '',
    
'server' => 'localhost',
    
'db' => 'my_database'
    
);

$test = new test($db_connection);
if (
count($test->ERR_get())>0) quit($test->ERR_get());
$user = $db_connection['login'];
$pwd = $db_connection['mdp'];
echo
"testing root user in table user :[".$test->SELECT_example($user,$pwd)."]<BR>";
if (
count($test->ERR_get())>0) quit($test->ERR_get());
echo
"insert user [".$test->INSERT_example("bob","dylan")."]<BR>";
if (
count($test->ERR_get())>0) quit($test->ERR_get());

function
quit($aErr)
{
 echo
"some errors occurs<BR>";
 
print_r($aErr);
 exit(
0);
}

/**************************************************************************
 **************************************************************************
 **************************************************************************
 ***************************************************************************/
class test
{
   
/**
     * @shortdesc : the database object
     * @type mixed
     **/
   
var $oDB;
   
/**
     * @shortdesc array of errors
     * @type array
     **/
   
var $aErr;

   
/**
     * @ builder
     * @param arrray dDSN: array with keys types specified (type, login, mdp, server, db)
     * @ type void
     * @public
     **/
  
function test($dDSN)
   {
    
$this->_ERR_ini();
    
$this->oDB = new database($dDSN);
   }
  
/**
    * get the error
    **/
  
function ERR_get()
   {
           return
array_merge($this->aErr,$this->oDB->ERR_get());
   }
  
   function
_ERR_ini()
   {
          
$this->aErr = array();
   }
  
/**
    * @shortdesc example of use of the SELECT with classe database
    * @param string user : user login
    * @param string pwd : login pwd
    * @type bool
    * @public
    **/
  
function SELECT_example($user,$pwd)
   {
    
$this->_ERR_ini();
       
$user = addslashes($user);
    
$pwd = md5($pwd);
    
$sSQL = "SELECT user_id FROM users WHERE user_login = '".$user."' and user_pwd = '".$pwd."'";
    
$this->oDB->execute($sSQL);
     if (
$this->ERR_get()) return;
     return (
$this->oDB->ENR_found()==1);
   }

  
/**
    * @shortdesc example of use of the INSERT with classe database
    * @param string user : user login
    * @param string pwd : login pwd
    * @type bool
    * @public
    **/
  
function INSERT_example($user,$pwd)
   {
    
$this->_ERR_ini();
       
$user = addslashes($user);
    
$pwd = md5($pwd);
    
$id = $this->oDB->getID('user');
    
$sSQL = "INSERT INTO users (user_id, user_login, user_pwd) VALUES ('".$id."','".$user."','".$pwd."')";
    
$this->oDB->execute($sSQL);
     if (
$this->ERR_get()) return;
     return (
$this->oDB->ENR_affected()==1);
   }
}
?>