fork download
  1. <?php
  2. namespace Procedural\Models;
  3. // в душе не ебу на самом деле как работать с этим расширением, пример чисто абстрактный
  4. $mysqli = mysqli_connect("example.com", "user", "password", "database");
  5.  
  6. function getAllUsers($mysqli)
  7. {
  8. $sql = "SELECT * FROM users";
  9. $sth = mysqli_query($mysqli, $sql);
  10. $res = mysqli_fetch_assoc($mysqli);
  11. return $res;
  12. }
  13.  
  14. function gettUserById($mysqli, $id)
  15. {
  16. $sql = "SELECT name, secondName, email FROM users WHERE id = $id";
  17. $sth = mysqli_query($mysqli, $sql);
  18. $res = mysqli_fetch_assoc($mysqli);
  19. return $res;
  20. }
  21.  
  22. //данные получаем так:
  23. $users = getAllUsers();
  24. $user = getUserById($id);
  25.  
  26. namespace OOP\Models;
  27.  
  28. //пилим класс-модель для юзера
  29.  
  30. class User
  31. {
  32. public $name;
  33. public $secondName;
  34. public $email;
  35. }
  36.  
  37. //пилим класс-шлюз для доступа к БД
  38.  
  39. class UsersTableGateaway
  40. {
  41. protected $dbh;
  42.  
  43. public function __construct()
  44. {
  45. //пилим подключение
  46. $this->dbh = new \PDO('mysql:host=localhost;dbname=test', $user, $pass);
  47. }
  48.  
  49. public function getAllUsers()
  50. {
  51. $sql = "SELECT * FROM users";
  52. $sth = $this->dbh->prepare("sql");
  53. $sth->execute();
  54. return $sth->fetchAll(\PDO::FETCH_CLASS, 'User');
  55.  
  56. }
  57.  
  58. public function getUserById($id)
  59. {
  60. $sql = "SELECT name, secondName, email FROM users WHERE id = $id";
  61. $sth = $this->dbh->prepare($sql);
  62. $sth->execute();
  63. return $sth->fetchAll(\PDO::FETCH_CLASS, 'User');
  64. }
  65. }
  66.  
  67. //данные получаем так:
  68. $gateaway = new UsersTableGateaway();
  69. $users = $gateaway->getAllUsers;
  70. $user = $gateaway->geatUserById($id);
Runtime error #stdin #stdout #stderr 0.01s 82880KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
PHP Fatal error:  Uncaught Error: Call to undefined function Procedural\Models\mysqli_connect() in /home/6Fcl6U/prog.php:4
Stack trace:
#0 {main}
  thrown in /home/6Fcl6U/prog.php on line 4