PHP Classes

File: spdo.sample.php

Recommend this page to a friend!
  Classes of Michal Vrchota   Singleton PDO   spdo.sample.php   Download  
File: spdo.sample.php
Role: Example script
Content type: text/plain
Description: Sample code
Class: Singleton PDO
Create a singleton instance of PDO class
Author: By
Last change:
Date: 18 years ago
Size: 989 bytes
 

Contents

Class file image Download
<?php

require_once("spdo.php");

function
foo()
{
   
/*
     * get single global instance
     */
    
   
$pdo = SPDO::getInstance();
   
   
   
   
/*
     * sample query
     */
   
   
echo "<pre>";
   
$stmt = $pdo->query("SELECT * FROM `users`");
   
    foreach(
$stmt as $row)
    {
       
print_r($row);
    }
    echo
"</pre>";
   
   
   
   
   
   
/*
     * modifyTable
     */
   
   
    // fill values to assoc. array
   
$set = array();
   
$set["field1"] = "value1";
   
$set["field2"] = 5;
   
$set["field3"] = NULL;
   
   
   
// insert new line
   
$pdo->modifyTable("test", $set);
   
// SQL: INSERT INTO `test` (field1, field2, field3) VALUES ('value1', '5', NULL)
   
    // update
   
$pdo->modifyTable("test", $set, "id = '5'");
   
// SQL: UPDATE `test` SET field1 = 'value1', field2 = '5', field3 = NULL WHERE id = '5'
   
}

try
{
   
$pdo = SPDO::connect("mysql:dbname=test;host=localhost", "root", "root");
   
$pdo->exec("SET NAMES 'utf8'");

   
foo();
}
catch(
Exception $e)
{
    echo
$e->getMessage();
}



?>