PHP Classes

File: demo.php

Recommend this page to a friend!
  Classes of Cedric Maenetja   Database Handler Package   demo.php   Download  
File: demo.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Database Handler Package
Prepare and execute SQL queries with MySQL
Author: By
Last change:
Date: 1 year ago
Size: 1,824 bytes
 

Contents

Class file image Download
<?php

   
require ('autoloader.php');

   
$dbhandler = new DatabaseHandler();

   
// PREPARE SINGLE SQL STATEMENT
   
$sql = $dbhandler->prepareStatement ('SELECT * FROM table WHERE id = ? AND email = ?');
    if (
App\Custom\Error::IsAnError ($sql))
    {
        die (
'Error: '.$sql->GetError());
    }

   
$sql = $dbhandler->executeStatement ([6, 'young.cet@gmail.com'], 'is');
    if (
App\Custom\Error::IsAnError ($sql))
    {
        die (
'Error: '.$sql->GetError());
    }

   
####################################################################################################################

    // PREPARING MULTIPLE QUERIES
   
$queries =
        [
           
'SELECT_ALL_USERS' => 'SELECT * FROM table',
           
'SELECT_USER' => 'SELECT * FROM table WHERE id = ?',
           
'INSERT_USER' => 'INSERT INTO table (fname, lname, email) VALUES (?,?,?)'
       
];

   
$sql = $dbhandler->loadQueries ($queries);
    if (
App\Custom\Error::IsAnError ($sql))
    {
        die (
'Error: '.$sql->GetError());
    }

   
$sql = $dbhandler->executeLoadedQuery ('SELECT_ALL_USERS', [], ''); // argument 2 & 3 are empty since this statement does not have a where clause
   
if (App\Custom\Error::IsAnError ($sql))
    {
        die (
'Error: '.$sql->GetError());
    }

   
// // fetch the results
   
print_r($dbhandler->fetchAll());

   
$sql = $dbhandler->executeLoadedQuery ('SELECT_USER', [6], 'i');
    if (
App\Custom\Error::IsAnError ($sql))
    {
        die (
'Error: '.$sql->GetError());
    }

   
// fetch the results
   
print_r($dbhandler->fetchRow());
   
   
$sql = $dbhandler->executeLoadedQuery ('INSERT_USER', ['yung', 'cet', 'young.cet@gmail.com'], 'sss');
    if (
App\Custom\Error::IsAnError ($sql))
    {
        die (
'Error: '.$sql->GetError());
    }

   
$dbhandler->closeConnection();
?>