Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.13 KB | None | 0 0
  1. <?php
  2.  
  3. class Database {
  4.  
  5.     private $conn;
  6.     private $result;
  7.    
  8.     function __construct(){
  9.        
  10.         //establish database connection
  11.        
  12.         $dbHost = 'localhost';
  13.         $dbUser = 'root';
  14.         $dbPass = '';
  15.         $dbName = 'test';
  16.        
  17.         $this->conn = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die (mysql_error('Error connecting to the database'));
  18.         mysql_select_db("$dbName") or die(mysql_error('Error in Selecting Database'));
  19.  
  20.         return $this->conn;
  21.     }
  22.    
  23.     function execute(){
  24.         $query = $this->sql;
  25.         $this->result = mysql_query($query) or die(mysql_error('error in retrieving records'));
  26.     }
  27.    
  28.     function fetch_array(){
  29.    
  30.         //retrieve records
  31.         $this->execute();
  32.         while($fetch = mysql_fetch_array($this->result)){
  33.            
  34.                 print_r($fetch);
  35.         }
  36.        
  37.     }
  38.    
  39.     function fetch_row(){
  40.    
  41.         $this->execute();
  42.        
  43.         while($rows = mysql_fetch_row($this->result)){
  44.            
  45.             print_r($rows);
  46.        
  47.         }
  48.     }
  49.    
  50.     //close database connection
  51.    
  52.     function __destruct(){
  53.    
  54.         mysql_close($this->conn);
  55.  
  56.     }
  57.  
  58. }
  59.  
  60.  
  61.     $data = new Database();
  62.     $data->sql = "SELECT * FROM employees ORDER BY id";
  63.     $data->fetch_array();
  64.     echo "</br>";
  65.     $data->fetch_row();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement