PHP Classes

File: eg.php

Recommend this page to a friend!
  Classes of Md. Shahadat Hossain Khan Razon   Extended PHP PDO   eg.php   Download  
File: eg.php
Role: Example script
Content type: text/plain
Description: Example
Class: Extended PHP PDO
Extend the PDO class to provide enhanced features
Author: By
Last change:
Date: 10 years ago
Size: 4,944 bytes
 

Contents

Class file image Download
<?php

include_once dirname(__FILE__).'/extendedpdo.cls.php';

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);



?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>Extended PDO Example</title></head>
<body><h2>Extended PDO Example</h2>
<?php


$db
=new ExtendedPDO('localhost', 'root', 'my password', 'my database name');
#$db->setDebugLevel(1);

echo '<h3>current selected database: '.$db->getCurrentDatabase().'</h3>';
$db->setCurrentTable('ri_customers');
echo
'<h3>current selected table: '.$db->getCurrentTable().'</h3>';

$data['user_id_of_outlet_owner']='razon';
$data['user_id_of_customer']='sumon';
$data['customer_name']='razon khan ['.date('r').']';
$data['customer_address']='n/a';
$data['customer_contact1']='sonadanga';
$data['customer_contact2']='khulna';

$insertedid=$db->sqlCreateUpdate($data);
if(
$insertedid){
   
$customerdata=$db->read($insertedid);
   
#$customerdata=$db->getRecordAssoc($insertedid);
   
echo '<pre>'.print_r($customerdata, true).'</pre>';
   
$customerdata=$db->getRecordAssocFromSQL('select * from `ri_customers` where `customer_id`='.$insertedid);
    echo
'<h3>data using $db->getRecordAssocFromSQL(select statement)</h3>';
    echo
'<pre>'.print_r($customerdata, true).'</pre>';

   
$rs=$db->query('select * from `ri_customers` where `customer_id`='.$insertedid.' limit 1');
    echo
'<h3>variable test: '.$rs->razon.'</h3>';
    if(
$rs && $rs->num_rows>0){
        echo
'<h4>Fetch Array</h4>';
       
$customerdata=$db->sqlFetchArray($rs);
        echo
'<pre>'.print_r($customerdata, true).'</pre>';
    }else echo
'<h3 style="color: #f00;">data not found for id - '.$insertedid.'</h3>';
   
   
$rs=$db->query('select * from `ri_customers` where `customer_id`='.$insertedid.' limit 1');
    if(
$rs && $rs->num_rows>0){
        echo
'<h4>Fetch Row</h4>';
       
$customerdata=$db->sqlFetchRow($rs);
        echo
'<pre>'.print_r($customerdata, true).'</pre>';
    }else echo
'<h3 style="color: #f00;">data not found for id - '.$insertedid.'</h3>';
   
   
$rs=$db->query('select * from `ri_customers` where `customer_id`='.$insertedid.' limit 1');
    if(
$rs && $rs->num_rows>0){
        echo
'<h4>Fetch Assoc</h4>';
       
$customerdata=$db->sqlFetchAssoc($rs);
        echo
'<pre>'.print_r($customerdata, true).'</pre>';
    }else echo
'<h3 style="color: #f00;">data not found for id - '.$insertedid.'</h3>';
   
   
$rs=$db->query('select * from `ri_customers` where `customer_id`='.$insertedid.' limit 1');
    if(
$rs && $rs->num_rows>0){
        echo
'<h4>Fetch Object</h4>';
       
$customerdata=$db->sqlFetchObject($rs);
        echo
'<pre>'.print_r($customerdata, true).'</pre>';
    }else echo
'<h3 style="color: #f00;">data not found for id - '.$insertedid.'</h3>';

}else echo
'<h2 style="color: #f00;">No data inserted due to error!</h2>';

echo
'<h3>total number of rows into '.$db->getCurrentTable().' - '.$db->getNumberOfRows().'</h3>';
echo
'<h3>total number of rows into ri_products - '.$db->getNumberOfRows(NULL, 'ri_products').'</h3>';
echo
'<h3>like operator sql test: '.ExtendedMySQLi::getSqlForLikeOperator('customer_name', 'razon,sumon, karim', 'or').'</h3>';
echo
'<h3>today is - '.ExtendedMySQLi::getMysqlFormatedDateTime().', date part of today is - '.ExtendedMySQLi::getMysqlFormatedDate().', time part of today is - '.ExtendedMySQLi::getMysqlFormatedTime().'</h3>';
echo
'<h3>is it valid date time format? '.(ExtendedMySQLi::isValidDateTime('2001-05-08 15:15:18')?'yes':'no').'</h3>';
echo
'<h3>is it valid date format? '.(ExtendedMySQLi::isValidDateTime('2001-05-08', true, false)?'yes':'no').'</h3>';
echo
'<h3>is it valid time format? '.(ExtendedMySQLi::isValidDateTime('25:15:18', false, true)?'yes':'no').'</h3>';

# encryption & decryption
function testEncryptDecrypt($piDb, $str2encrypt, $encryptsalt){
   
$encrypted=$piDb->aesEncrypt($str2encrypt, $encryptsalt);
   
$decrypted=$piDb->aesDecrypt($encrypted, $encryptsalt);
    echo
'<h3>Data to encrypt: '.$str2encrypt.', Salt for encryption: '.(isset($encryptsalt)?$encryptsalt:'NULL').', Encrypted: '.$encrypted.', Decrypted: '.$decrypted.', Encryption Status: '.($decrypted==$str2encrypt?'OK':'Not OK').'</h3>';
}
testEncryptDecrypt($db, '654321', 'razon');
testEncryptDecrypt($db, '6543210', 'razon');
testEncryptDecrypt($db, '654321', 'razon'.time());
testEncryptDecrypt($db, 'sumOn321', 'my secret key');

# reading from database
echo '<h3>Available Customers using $db->getAllRows(...)</h3><pre>'.print_r($db->getAllRows('select customer_id, user_id_of_outlet_owner, customer_name from ri_customers'), true).'</pre>';
echo
'<h3>Available Customers using $db->getRows(..., ...)</h3><pre>'.print_r($db->getRows('customer_id', 'select customer_id, user_id_of_outlet_owner, customer_name from ri_customers'), true).'</pre>';
echo
'<h3>Available Customers using $db->getArrWithIdAsKey(...)</h3><pre>'.print_r($db->getArrWithIdAsKey('select customer_id, customer_name from ri_customers'), true).'</pre>';

?>
</body>
</html>