Advertisement
Guest User

Untitled

a guest
Oct 14th, 2017
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. # Basic CRUD operations with PDO
  2.  
  3. CRUD = Create, Read, Update, Delete
  4.  
  5. ## Open a database connection
  6.  
  7. ```php
  8. $host = '127.0.0.1';
  9. $dbname = 'test';
  10. $username = 'root';
  11. $password = '';
  12. $charset = 'utf8';
  13. $collate = 'utf8_unicode_ci';
  14. $dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";
  15. $options = [
  16. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  17. PDO::ATTR_PERSISTENT => false,
  18. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  19. PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES $charset COLLATE $collate"
  20. ];
  21.  
  22. $pdo = new PDO($dsn, $username, $password, $options);
  23. ```
  24.  
  25. ## Select a single row
  26.  
  27. ```php
  28. $stmt = $pdo->prepare('SELECT * FROM users WHERE email = :email AND status=:status LIMIT 1');
  29. $stmt->execute(['email' => $email, 'status' => $status]);
  30. $user = $stmt->fetch();
  31. ```
  32.  
  33. ## Select multiple rows
  34.  
  35. With fetch for large results.
  36.  
  37. ```php
  38. $stmt = $pdo->query('SELECT name FROM users');
  39. while ($row = $stmt->fetch()) {
  40. echo $row['name'] . "\n";
  41. }
  42.  
  43. // or even simpler
  44. foreach ($stmt as $row) {
  45. echo $row['name'] . "\n";
  46. }
  47. ```
  48.  
  49. With fetchAll for small results.
  50.  
  51. ```php
  52. $news = $pdo->query('SELECT * FROM news')->fetchAll();
  53. ```
  54.  
  55. ## Insert a single row
  56.  
  57. ```php
  58. $row = [
  59. 'username' => 'bob',
  60. 'email' => 'bob@example.com'
  61. ];
  62. $sql = "INSERT INTO users SET username=:username, email=:email;";
  63. $status = $pdo->prepare($sql)->execute($row);
  64.  
  65. if ($status) {
  66. $lastId = $pdo->lastInsertId();
  67. echo $lastId;
  68. }
  69. ```
  70.  
  71. ## Insert multiple rows
  72.  
  73. ```php
  74. $rows = [];
  75. $rows[] = [
  76. 'username' => 'bob',
  77. 'email' => 'bob@example.com'
  78. ];
  79. $rows[] = [
  80. 'username' => 'max',
  81. 'email' => 'max@example.com'
  82. ];
  83.  
  84. $sql = "INSERT INTO users SET username=:username, email=:email;";
  85. $stmt = $pdo->prepare($sql);
  86. foreach ($rows as $row) {
  87. $stmt->execute($row);
  88. }
  89. ```
  90.  
  91. ## Update a single row
  92.  
  93. ```php
  94. $row = [
  95. 'id' => 1,
  96. 'username' => 'bob',
  97. 'email' => 'bob2@example.com'
  98. ];
  99. $sql = "UPDATE users SET username=:username, email=:email WHERE id=:id;";
  100. $status = $pdo->prepare($sql)->execute($row);
  101. ```
  102.  
  103. ## Update multiple rows
  104.  
  105. ```php
  106. $row = [
  107. 'updated_at' => '2017-01-01 00:00:00'
  108. ];
  109. $sql = "UPDATE users SET updated_at=:updated_at";
  110. $pdo->prepare($sql)->execute($row);
  111.  
  112. $affected = $pdo->rowCount();
  113. ```
  114.  
  115. ## Delete a single row
  116.  
  117. ```php
  118. $where = ['id' => 1];
  119. $pdo->prepare("DELETE FROM users WHERE id=:id")->execute($where);
  120. ```
  121.  
  122. ## Delete multiple rows
  123.  
  124. ```php
  125. $pdo->prepare("DELETE FROM users")->execute();
  126. ```
  127.  
  128. ## PDO datatypes
  129.  
  130. Getting dynamic (POST) data with null values can be difficult to handle with PDO.
  131. Here is a helper function to detect the correct data type.
  132.  
  133. ```php
  134. function get_pdo_type($value)
  135. {
  136. switch (true) {
  137. case is_bool($value):
  138. $dataType = PDO::PARAM_BOOL;
  139. break;
  140. case is_int($value):
  141. $dataType = PDO::PARAM_INT;
  142. break;
  143. case is_null($value):
  144. $dataType = PDO::PARAM_NULL;
  145. break;
  146. default:
  147. $dataType = PDO::PARAM_STR;
  148. }
  149. return $dataType;
  150. }
  151.  
  152. // Usage
  153. $email = $_POST['email'];
  154.  
  155. $pdo = new PDO('dsn', 'username', 'password');
  156. $sql = 'INSERT INTO users SET email=:email;';
  157. $stmt = $pdo->prepare($sql);
  158. $stmt->bindValue(':email', $email, get_pdo_type($email));
  159. $stmt->execute();
  160. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement