PHP Classes

File: test.mysql.php

Recommend this page to a friend!
  Classes of Jason Gerfen   PDO MySQLi MySQL Class   test.mysql.php   Download  
File: test.mysql.php
Role: Example script
Content type: text/plain
Description: Global interface for PDO/MySQLi/MySQL connections. Quietly degrades support based on available classes.
Class: PDO MySQLi MySQL Class
Access MySQL using MySQL, MySQLi or PDO extensions
Author: By
Last change: Same thing
Date: 13 years ago
Size: 1,246 bytes
 

Contents

Class file image Download
<?php
 
/* Does our class exist? */
 
if (file_exists('class.mysql.php')) {
  include
'class.mysql.php';

 
/* User defined connection settings
     PDO DSN created dynamically
  */
 
$settings = array('server'=>'localhost',
                   
'username'=>'username',
                   
'password'=>'password',
                   
'database'=>'database');

 
/* Singleton object (PDO/MySQLi/MySQL)
     Gracefully degrades access method based on
     MySQL extensions loaded.
  */
 
$db = dbconn::instance($settings);

 
/* Safe and sanitized dynamic SQL statement */
 
$sql = sprintf('SELECT * FROM `table` WHERE `field1` LIKE "%s" AND `field2`
                 LIKE "%d" LIMIT 1'
, $db->sanitize($string), $db->sanitize($integer));

 
/* Results of SQL statement */
 
$results = $db->query($sql);

 
/* Error? */
 
if (!$results) {
   echo
$db->error;
  }

 
$count = $db->affected($db);
  if (
$count>=1) {
  
$results = $db->results($results);
  }
  echo
'Records found: '.$count.'<pre>'; print_r($results); echo '</pre><hr/>';

 
/* No need to close or flush connection, the
     __destruct() will repair, optimize and
     re-index entire database prior to removing
     singleton object
  */
}
?>