Guest User

Untitled

a guest
Mar 10th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. <?php
  2. ini_set('display_errors', '1');
  3. error_reporting(E_ALL);
  4. header('Content-Type: text/html; charset=utf-8');
  5.  
  6. class Db {
  7. private $host;
  8. private $db;
  9. private $user;
  10. private $pass;
  11. private $charset;
  12.  
  13. public function __construct()
  14. {
  15. $this->host = 'localhost';
  16. $this->db = 'task9';
  17. $this->user = 'root';
  18. $this->pass = '';
  19. $this-> charset = 'utf8';
  20. }
  21. public function dbConnect()
  22. {
  23. $dsn = "mysql:host=$this->host;dbname=$this->db;charset=$this->charset";
  24. $opt = [
  25. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  26. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  27. PDO::ATTR_EMULATE_PREPARES => false,
  28. ];
  29. $this->pdo = new PDO($dsn, $this->user, $this->pass, $opt);
  30. }
  31. public function update()
  32. {
  33. $stmt = $this->pdo->prepare('UPDATE users SET name = :name, surname = :surname,age = :age WHERE id = :id');
  34. $result = $stmt->execute(array('id'=>$_POST['id'], 'name'=>$_POST['name'],'surname'=>$_POST['surname'],'age'=>$_POST['age']));
  35. return $result;
  36. }
  37. public function insert()
  38. {
  39. $stmt = $this->pdo->prepare('INSERT INTO users SET name = :name, surname = :surname,age = :age');
  40. $result = $stmt->execute(array('name'=>$_POST['name'],'surname'=>$_POST['surname'],'age'=>$_POST['age']));
  41. return $result;
  42. }
  43. public function delete($id)
  44. {
  45. $stmt = $this->pdo->prepare('DELETE FROM users WHERE id = :id');
  46. $result = $stmt->execute(['id'=>$id]);
  47. return $result;
  48. }
  49. public function selectOneItem($id)
  50. {
  51. $stmt = $this->pdo->prepare('SELECT * FROM users WHERE id = :id');
  52. $stmt->execute(array('id'=>$id));
  53. $user = $stmt-> fetch();
  54. return $user;
  55. }
  56. public function selectByAge($age)
  57. {
  58. $stmt = $this->pdo->prepare('SELECT * FROM users WHERE age = :age');
  59. $stmt->execute(array('age'=>$age));
  60. $users = $stmt-> fetchAll();
  61. return $users;
  62. }
  63. public function selectAll()
  64. {
  65. $stmt = $this->pdo->prepare('SELECT * FROM users');
  66. $stmt->execute(array());
  67. $users = $stmt-> fetchAll();
  68. return $users;
  69. }
  70.  
  71. }
  72. ?>
Add Comment
Please, Sign In to add comment