Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.55 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 = 'school';
  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.         function insert($data){
  52.    
  53.    
  54.             $fields = implode(',',$data);
  55.  
  56.             $query = "INSERT INTO students SET $fields";
  57.             echo $query;
  58.             $result = mysql_query($query) or die( mysql_error());
  59.             echo $result;
  60.             return $result;
  61.     }
  62.    
  63.     function __destruct(){
  64.    
  65.         mysql_close($this->conn);
  66.  
  67.     }
  68.    
  69.  
  70.  
  71. }
  72.  
  73.  
  74.     $d = new Database();
  75.    
  76.     $fname = $_POST['first_name'];
  77.     $lname = $_POST['last_name'];
  78.     $eadd = $_POST['email_address'];
  79.     $mobile = $_POST['mobile_number'];
  80.    
  81.     $data = array(
  82.             'first_name' => $fname,
  83.             'last_name' => $lname,
  84.             'email_address' => $eadd,
  85.             'mobile_number' => $mobile
  86.     );
  87.    
  88.     $d->insert($data);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement