Guest User

Untitled

a guest
Nov 21st, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. class Database {
  2.  
  3. /* Properties */
  4. private $conn;
  5. private $dsn = 'mysql:dbname=test;host=127.0.0.1';
  6. private $user = 'root';
  7. private $password = '';
  8.  
  9. /* Creates database connection */
  10. public function __construct() {
  11. try {
  12. $this->conn = new PDO($this->dsn, $this->user, $this->password);
  13. } catch (PDOException $e) {
  14. print "Error!: " . $e->getMessage() . "";
  15. die();
  16. }
  17. return $this->conn;
  18. }
  19. }
  20.  
  21. include "database.php";
  22.  
  23. class User {
  24. /* Properties */
  25. private $conn;
  26.  
  27. /* Get database access */
  28. public function __construct() {
  29. $this->conn = new Database();
  30. }
  31.  
  32. /* Login a user */
  33. public function login() {
  34. $stmt = $this->conn->prepare("SELECT username, usermail FROM user");
  35. if($stmt->execute()) {
  36. while($rows = $stmt->fetch()) {
  37. $fetch[] = $rows;
  38. }
  39. return $fetch;
  40. }
  41. else {
  42. return false;
  43. }
  44. }
  45. }
  46.  
  47. $user = new User();
  48. $list = $user->login();
  49.  
  50. foreach($list as $test) {
  51. echo $test["username"];
  52. }
  53.  
  54. <?php
  55. $host = '127.0.0.1';
  56. $db = 'test';
  57. $user = 'root';
  58. $pass = '';
  59. $charset = 'utf8';
  60.  
  61. $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
  62. $opt = [
  63. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  64. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  65. PDO::ATTR_EMULATE_PREPARES => false,
  66. ];
  67. $pdo = new PDO($dsn, $user, $pass, $opt);
  68.  
  69. <?php
  70. class User {
  71. /* Properties */
  72. private $conn;
  73.  
  74. /* Get database access */
  75. public function __construct($pdo) {
  76. $this->conn = $pdo;
  77. }
  78.  
  79. /* List all users */
  80. public function get_users() {
  81. return $this->conn->query("SELECT username, usermail FROM user")->fetchAll();
  82. }
  83. }
  84.  
  85. include 'database.php';
  86. $user = new User($pdo);
  87. $list = $user->get_users();
  88.  
  89. foreach($list as $test) {
  90. echo $test["username"];
  91. }
  92.  
  93. username_foo
  94. username_bar
  95. username_baz
  96.  
  97. <?php
  98.  
  99. class Database {
  100.  
  101. /* Properties */
  102. private $conn;
  103. private $dsn = 'mysql:dbname=test;host=127.0.0.1';
  104. private $user = 'root';
  105. private $password = '';
  106.  
  107. /* Creates database connection */
  108. public function __construct() {
  109. }
  110.  
  111. public function getConnection() {
  112. try {
  113. $this->conn = new PDO($this->dsn, $this->user, $this->password);
  114. } catch (PDOException $e) {
  115. print "Error!: " . $e->getMessage() . "";
  116. die();
  117. }
  118. return $this->conn;
  119. }
  120. }
  121. ?>
Add Comment
Please, Sign In to add comment