Advertisement
haikelfazzani

Php OOP

Aug 19th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.58 KB | None | 0 0
  1. // connection database
  2.  
  3. <?php
  4.  
  5.     class connectDb{
  6.  
  7.         private $hostName   = 'localhost';
  8.         private $dbName     = 'php_tut';
  9.         private $dbUser     = 'root';
  10.         private $dbPassword = '';
  11.         private $conn       = false;
  12.  
  13.         function connect(){
  14.  
  15.             try{
  16.  
  17.                 $this->conn = new PDO("mysql:host=$this->hostName;dbname=$this->dbName","$this->dbUser","$this->dbPassword");
  18.  
  19.                 return $this->conn;
  20.  
  21.             }catch(PDOExeception $e){
  22.  
  23.                 return die("error : " . $e->getMessage());
  24.             }
  25.  
  26.         }
  27.  
  28.         function disconnect(){
  29.             return $this->conn = NULL;
  30.         }
  31.  
  32.     }
  33.  
  34. // class user : function insert user into database
  35.  
  36.     require_once("connection.php");
  37.  
  38.     class Users implements userFunctions{
  39.  
  40.         protected $username;
  41.         protected $password;
  42.         protected $level;
  43.         protected $town;
  44.         protected $country;
  45.  
  46.         function insertUser($username,$password,$level,$town,$country){
  47.  
  48.             $this->username = $username;
  49.             $this->password = $password;
  50.             $this->level = $level;
  51.             $this->town = $town;
  52.             $this->country = $country;
  53.  
  54.             $req = "INSERT INTO users(username,password,level,town,country)
  55.             VALUES(:username,:password,:level,:town,:country)";
  56.  
  57.             $db = new connectDB();
  58.             $conn = $db->connect();
  59.  
  60.             $stmt = $conn->prepare($req);
  61.  
  62.             $stmt->bindValue(":username",$this->username,PDO::PARAM_STR);
  63.             $stmt->bindValue(":password",$this->password,PDO::PARAM_STR);
  64.             $stmt->bindValue(":level",$this->level,PDO::PARAM_STR);
  65.             $stmt->bindValue(":town",$this->town,PDO::PARAM_STR);
  66.             $stmt->bindValue(":country",$this->country,PDO::PARAM_STR);
  67.  
  68.             $stmt->execute();
  69.  
  70.             echo "record added";
  71.  
  72.         }
  73.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement