Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 10th, 2012  |  syntax: None  |  size: 2.50 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Using PDO database class within other PHP classes
  2. class db {
  3. private $host;
  4. private $username;
  5. private $password;
  6. private $con;
  7.     private $pdo;
  8.  
  9. public function __construct( $database = "dnname" )
  10. {
  11.     $this->host = "localhost";
  12.     $this->username = "username";
  13.     $this->password = "pass";
  14.             $conStr = "host={$this->host};dbname={$database}";
  15.             try {
  16.                 $this->pdo = new PDO( "mysql:$conStr", $this->username, $this->password );
  17.                 $this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  18.             }
  19.             catch( PDOException $e ) {
  20.                 echo "error ". $e->getMessage();
  21.             }
  22. }
  23.  
  24. public function fetch_single_row($sql, $data)
  25. {
  26.     if( $data !== null )
  27.         $data = array_values( $data ); // Incase it's an associative array
  28.     $sel = $this->pdo->prepare( $sql );
  29.     $sel->execute( $data );
  30.     $sel->setFetchMode( PDO::FETCH_OBJ );
  31.     $obj = $sel->fetch();
  32.     return $obj;
  33. }
  34.        
  35. class cms {
  36.    function cms(){
  37.        $this->db = new db();
  38.    }
  39.  
  40.    function is_admin($id) {
  41.     if($this->db->fetch_single_row("SELECT id FROM user WHERE id = ? LIMIT 1", array($id))){
  42.         return true;
  43.     } else {
  44.         return false;
  45.     }
  46. }
  47.        
  48. include("db.class.php");
  49. include("cms.class.php");
  50.  
  51. $cms = new cms();
  52.  
  53. if($cms->is_admin($id)){
  54.    //code here
  55. }
  56.        
  57. abstract class DB
  58.    {
  59.  
  60.     protected static $instance;
  61.  
  62.     protected $db;
  63.  
  64.     protected static $host = 'host';
  65.     protected static $user = 'user';
  66.     protected static $pass = 'pass';
  67.     protected static $database;
  68.  
  69.     public static function getInstance()
  70.     {
  71.         if (!isset(self::$instance)) self::$instance = new static();
  72.  
  73.         return self::$instance;
  74.     }
  75.  
  76.     protected function __construct()
  77.     {
  78.         $this->db = new PDO(sprintf('mysql:host=%s;dbname=%s', static::$host, static::$database), static::$user, static::$pass);
  79.         $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  80.     }
  81.  
  82.     public static function db()
  83.     {
  84.         return static::getInstance()->db;
  85.     }
  86.  
  87.     public function fetch_single_row($sql, $data)
  88.     {
  89.         if( $data !== null )
  90.             $data = array_values( $data ); // Incase it's an associative array
  91.         $sel = self::db()->prepare( $sql );
  92.         $sel->execute( $data );
  93.         $sel->setFetchMode( PDO::FETCH_OBJ );
  94.         $obj = $sel->fetch();
  95.         return $obj;
  96.     }
  97. }
  98.        
  99. class Main extends DB
  100. {
  101.     protected static $database = 'db';
  102. }
  103.        
  104. $db = Main::getInstance();
  105. $obj = $db->fetch_single_row($sql, $data);