Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ini_set('display_errors', '1');
- error_reporting(E_ALL);
- header('Content-Type: text/html; charset=utf-8');
- class Db {
- private $host;
- private $db;
- private $user;
- private $pass;
- private $charset;
- public function __construct()
- {
- $this->host = 'localhost';
- $this->db = 'task9';
- $this->user = 'root';
- $this->pass = '';
- $this-> charset = 'utf8';
- }
- public function dbConnect()
- {
- $dsn = "mysql:host=$this->host;dbname=$this->db;charset=$this->charset";
- $opt = [
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
- PDO::ATTR_EMULATE_PREPARES => false,
- ];
- $this->pdo = new PDO($dsn, $this->user, $this->pass, $opt);
- }
- public function update()
- {
- $stmt = $this->pdo->prepare('UPDATE users SET name = :name, surname = :surname,age = :age WHERE id = :id');
- $result = $stmt->execute(array('id'=>$_POST['id'], 'name'=>$_POST['name'],'surname'=>$_POST['surname'],'age'=>$_POST['age']));
- return $result;
- }
- public function insert()
- {
- $stmt = $this->pdo->prepare('INSERT INTO users SET name = :name, surname = :surname,age = :age');
- $result = $stmt->execute(array('name'=>$_POST['name'],'surname'=>$_POST['surname'],'age'=>$_POST['age']));
- return $result;
- }
- public function delete($id)
- {
- $stmt = $this->pdo->prepare('DELETE FROM users WHERE id = :id');
- $result = $stmt->execute(['id'=>$id]);
- return $result;
- }
- public function selectOneItem($id)
- {
- $stmt = $this->pdo->prepare('SELECT * FROM users WHERE id = :id');
- $stmt->execute(array('id'=>$id));
- $user = $stmt-> fetch();
- return $user;
- }
- public function selectByAge($age)
- {
- $stmt = $this->pdo->prepare('SELECT * FROM users WHERE age = :age');
- $stmt->execute(array('age'=>$age));
- $users = $stmt-> fetchAll();
- return $users;
- }
- public function selectAll()
- {
- $stmt = $this->pdo->prepare('SELECT * FROM users');
- $stmt->execute(array());
- $users = $stmt-> fetchAll();
- return $users;
- }
- }
- ?>
Add Comment
Please, Sign In to add comment