Advertisement
Dotunoyesanmi

PDO

May 3rd, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.08 KB | None | 0 0
  1. <?php
  2.     class db {
  3.         private $dsn = 'mysql:host=127.0.0.1;dbname=users';
  4.         private  $username    =  'root';
  5.         private  $password   =   '';
  6.         public $dbh;
  7.  
  8.         public function __construct(){
  9.             $this->conn();
  10.         }
  11.  
  12.         public function conn(){
  13.             try {
  14.                 $this->dbh = new PDO($this->dsn, $this->username, $this->password);
  15.                 $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  16.                 $this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  17.                 } catch (PDOException $e) {
  18.                     echo  "Database Connection failed because: " . $e->getMessage() . "<br/>";
  19.                     die();
  20.                     }
  21.         }
  22.  
  23.         public function run_query($name, $email) {
  24.             try{
  25.             $stmt = $this->dbh->prepare ("INSERT INTO users (name, email) VALUES (:name, :email)");
  26.             /*$stmt -> bindParam(':name', $name, PDO::PARAM_STR, 12);
  27.             $stmt -> bindParam(':email', $email, PDO::PARAM_STR, 12);*/
  28.             $stmt->execute(array($name, $email));
  29.            
  30.         } catch (PDOException $e) {
  31.             echo 'Query failed : '. $e->getMessage;
  32.         }
  33.         }
  34.         }
  35.  
  36.         $conn = new  db;
  37.         $conn->run_query('Dotun', 'dotunoyesanmi@gmail.com');
  38.  
  39.  
  40. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement