Advertisement
Guest User

Untitled

a guest
May 7th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.24 KB | None | 0 0
  1. <?php
  2.  
  3. interface idb
  4. {
  5.  
  6.     public function connect ( );
  7.  
  8.     public function fetch_assoc ( );
  9.  
  10.     public function query ( $sql );
  11. }
  12.  
  13. abstract class adb implements idb
  14. {
  15.  
  16.     protected $db_host = null;
  17.  
  18.     protected $db_user = null;
  19.  
  20.     protected $db_pass = null;
  21.  
  22.     protected $db_name = null;
  23.  
  24.     protected $db_link = null;
  25.  
  26.     public function __construct ( $db_host, $db_user, $db_pass, $db_name )
  27.     {
  28.  
  29.         $this->db_host = $db_host;
  30.         $this->db_user = $db_user;
  31.         $this->db_pass = $db_pass;
  32.         $this->db_name = $db_name;
  33.         $this->connect( );
  34.     }
  35. }
  36.  
  37. class db
  38. {
  39.  
  40.     protected $db = null;
  41.  
  42.     public function __construct ( $db_type, $db_host, $db_user, $db_pass, $db_name )
  43.     {
  44.  
  45.         $this->db = new $db_type( $db_host, $db_user, $db_pass, $db_name );
  46.     }
  47. }
  48.  
  49. class MYSQL extends adb
  50. {
  51.  
  52.     public function connect ( )
  53.     {
  54.  
  55.         mysql_connect( $this->db_host, $this->db_user, $this->db_pass );
  56.         mysql_select_db( $this->db_name );
  57.     }
  58.  
  59.     public function fetch_assoc ( )
  60.     {
  61.  
  62.         return mysql_fetch_assoc( $this->db_link );
  63.     }
  64.  
  65.     public function query ( $sql )
  66.     {
  67.  
  68.         mysql_query( $sql, $this->db_link );
  69.     }
  70. }
  71.  
  72. // Usage
  73. $db = new db( 'MYSQL', 'localhost', 'root', '', 'localhost' );
  74. $db->query( 'SELECT * FROM table' );
  75. $result = $db->fetch_assoc( );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement