PHP Classes

File: examples.php

Recommend this page to a friend!
  Packages of abdul rashid   PHP PDO Singleton Database Wrapper   examples.php   Download  
File: examples.php
Role: Example script
Content type: text/plain
Description: Example File for usage of the Database class
Class: PHP PDO Singleton Database Wrapper
Execute common MySQL queries using PDO
Author: By
Last change:
Date: 14 years ago
Size: 3,245 bytes
 

 

Contents

Class file image Download
<?php
    
    define("DB_HOST","");
    define("DB_USERNAME","");
    define("DB_PASSWORD","");
    define("DB_NAME","");
    define("DB_PORT","");
    define("TBL_NAME1","");

    require('Database.singleton.php');

    $tmp_obj_db = Database::obtain(DB_HOST, DB_USERNAME, DB_PASSWORD,DB_NAME);
        
    if($tmp_obj_db->connect_pdo() === false)
    {
            echo "There was problem in connecting to the database.<BR>".$tmp_obj_db->getError();
            return  "";
    }
    
    /* START: simple way to insert a record into DB */
    
    $data = array(
    'col_2' => 'val_2',
    'col_3' => 'val_3',
    'col_4' => 5
    );

    $tmp_int_last_id = $tmp_obj_db->insert_pdo(TBL_NAME1,$data);
    
    if($tmp_int_last_id === false)
    {
            echo "There was problem in inserting the record.<BR>".$tmp_obj_db->getError();
            return  "";        
    }
    print("Last Insert ID ==> " . $tmp_int_last_id);
    /* END: simple way to insert a record into DB */



    /* START: simple way to update a record into DB */
    
    $data = array(
    'col_2' => NULL,
    'col_3' => 'val_3',
    'col_4' => 5
    );
    
    $where = array("col_4" =>5 , "col_3" => 'abc');

    $tmp_bln_rtn_update_status = $tmp_obj_db->update_pdo(TBL_NAME1,$data,$where);
    
    if($tmp_bln_rtn_update_status === false)
    {
            echo "There was problem in updating the record.<BR>".$tmp_obj_db->getError();
            return  "";        
    }
    echo "<BR>";
    print("UpdateStatus=> ". $tmp_bln_rtn_update_status);
    
    /* START: simple way to update a record into DB */
    
    
    
    /* START: example usage of query_first_pdo which is used to fetch one record from Database */
    
    $tmp_str_sql = "SELECT count(1) as cnt from " . TBL_NAME1 ." where col_3=? and col_4=?";
    
    $tmp_arr_queryParams[] =  'val_3';
    $tmp_arr_queryParams[] =  5; 
    
    $results = $tmp_obj_db->query_first_pdo($tmp_str_sql,$tmp_arr_queryParams);
    
    if($results === false)
    {
            echo "There was problem in fetching the record.<BR>".$tmp_obj_db->getError();
            return  "";        
    }    
    $tmp_int_total = (int)$results['cnt'];
    echo "<BR>";
    print("Total=> " . $tmp_int_total);
    
    /* END: example usage of query_first_pdo which is used to fetch one record from Database */    
    
    
    
    
    /* START: example usage of fetch_array_pdo which is used to fetch multiple records from Database */
    
    $tmp_arr_queryParams = Array();
    $tmp_str_sql = "SELECT * from " . TBL_NAME1 ." where col_3=? and col_4=?";
    
    $tmp_arr_queryParams[] =  'val_3';
    $tmp_arr_queryParams[] =  5; 
    
    $results = $tmp_obj_db->fetch_array_pdo($tmp_str_sql,$tmp_arr_queryParams);
    
    if($results === false)
    {
            echo "There was problem in fetching multiple record.<BR>".$tmp_obj_db->getError();
            return  "";        
    }    
    
    echo "<BR>";
    print("Records Fetched <BR><pre>");
    print_r($results);
    /* END: example usage of fetch_array_pdo which is used to fetch multiple records from Database */    
        
    
    

?>