Advertisement
Guest User

Untitled

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