Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Database {
- private $host = "localhost";
- private $user = "root";
- private $pass = "123456";
- private $dbname = "test";
- private $db;
- private $error;
- private $stmt;
- public function __construct() {
- $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
- $options = [
- PDO::ATTR_PERSISTENT => true,
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
- try {
- $this->db = new PDO($dsn, $this->user, $this->pass, $options);
- } catch (PDOException $e) {
- $this->error = $e->getMessage();
- }
- }
- public function query($query) {
- $this->stmt = $this->db->prepare($query);
- }
- public function bind($param, $value, $type = null) {
- if (is_null($type)) {
- switch (true) {
- case is_int($value):
- $type = PDO::PARAM_INT;
- break;
- case is_bool($value):
- $type = PDO::PARAM_BOOL;
- break;
- case is_null($value):
- $type = PDO::PARAM_NULL;
- break;
- default:
- $type = PDO::PARAM_STR;
- }
- }
- $this->stmt->bindValue($param, $value, $type);
- }
- public function execute() {
- return $this->stmt->execute();
- }
- }
- $data = new Database;
- $sql = " INSERT INTO users (name,email) VALUES (:name,:email)";
- $stmt = $data->query($sql);
- $stmt = $data->bind(':name', "John");
- $stmt = $data->bind(':email', "google@gmail.com");
- $stmt = $data->execute();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement