Advertisement
Guest User

Untitled

a guest
May 4th, 2017
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 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.  
  15. $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=$charset", $username, $password,
  16. array(
  17. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  18. PDO::ATTR_PERSISTENT => false,
  19. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  20. PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES $charset COLLATE $collate"
  21. )
  22. );
  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.  
  44. With fetchAll for small results.
  45.  
  46. ```php
  47. $news = $pdo->query('SELECT * FROM news')->fetchAll();
  48. ```
  49.  
  50. ## Insert a single row
  51.  
  52. ```php
  53. $row = [
  54. 'username' => 'bob',
  55. 'email' => 'bob@example.com'
  56. ];
  57. $sql = "INSERT INTO users SET username=:username, email=:email;";
  58. $status = $pdo->prepare($sql)->execute($row);
  59.  
  60. if ($status) {
  61. $lastId = $pdo->lastInsertId();
  62. echo $lastId;
  63. }
  64. ```
  65.  
  66. ## Insert multiple rows
  67.  
  68. ```php
  69. $rows = [];
  70. $rows[] = [
  71. 'username' => 'bob',
  72. 'email' => 'bob@example.com'
  73. ];
  74. $rows[] = [
  75. 'username' => 'max',
  76. 'email' => 'max@example.com'
  77. ];
  78.  
  79. $sql = "INSERT INTO users SET username=:username, email=:email;";
  80. $stmt = $pdo->prepare($sql);
  81. foreach ($rows as $row) {
  82. $stmt->execute($row);
  83. }
  84. ```
  85.  
  86. ## Update a single row
  87.  
  88. ```php
  89. $row = [
  90. 'id' => 1,
  91. 'username' => 'bob',
  92. 'email' => 'bob2@example.com'
  93. ];
  94. $sql = "UPDATE users SET username=:username, email=:email WHERE id=:id;";
  95. $status = $pdo->prepare($sql)->execute($row);
  96. ```
  97.  
  98. ## Update multiple rows
  99.  
  100. ```php
  101. $row = [
  102. 'updated_at' => '2017-01-01 00:00:00'
  103. ];
  104. $sql = "UPDATE users SET updated_at=:updated_at";
  105. $pdo->prepare($sql)->execute($row);
  106.  
  107. $affected = $pdo->rowCount();
  108. ```
  109.  
  110. ## Delete a single row
  111.  
  112. ```php
  113. $where = ['id' => 1];
  114. $pdo->prepare("DELETE FROM users WHERE id=:id")->execute($where);
  115. ```
  116.  
  117. ## Delete multiple rows
  118.  
  119. ```php
  120. $pdo->prepare("DELETE FROM users")->execute();
  121. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement