Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. <?php
  2.  
  3. $host = 'localhost';
  4. $user = 'root';
  5. $pass = '';
  6. $dbname = 'connecttutorial';
  7. $charset = 'utf8';
  8.  
  9. $dsn = "mysql:host={$host};dbname={$dbname};charset={$charset}";
  10.  
  11. $opt = [
  12. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  13. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  14. PDO::ATTR_EMULATE_PREPARES => false,
  15. ];
  16.  
  17. try{
  18. $pdo = new PDO($dsn, $user, $pass, $opt);
  19. }
  20. catch(PDOException $e) {
  21. die('Could not connect to the database using credentials given. Error code: '.$e->getCode());
  22. }
  23.  
  24. // CREATE -------------------------------------------------------------------------------------------
  25. try {
  26. $sql = "INSERT INTO books (collection, title, author) VALUES (?,?,?)";
  27. $pdo->prepare($sql)->execute(['World of Warcraft','War crimes', 'Christie Golden']);
  28.  
  29. $sql = "INSERT INTO books (collection, title, author) VALUES (:collection,:title,:author)";
  30. $pdo->prepare($sql)->execute(['collection' => 'Warcraft','title' => 'The Shattering', 'author' => 'Christie Golden']);
  31. // get id for the last insterted row
  32. $id = $pdo->lastInsertId();
  33. }
  34. catch(PDOException $e) {
  35. die('There was a problem when inserting. Returned message was: '.$e->getMessage());
  36. var_dump($e);
  37. }
  38.  
  39. // READ --------------------------------------------------------------------------------------------
  40. try {
  41. $stmt = $pdo->query("SELECT * FROM books ORDER BY author ASC, title ASC");
  42. foreach($stmt as $row) {
  43. echo $row['collection'].': '.$row['title'].' - '.$row['author'].'<br />';
  44. }
  45. }
  46. catch (PDOException $e) {
  47. die('There was a problem when trying to read from table. Returned message was: '.$e->getMessage());
  48. }
  49.  
  50. // READ ONE ROW ------------------------------------------------------------------------------------
  51. try {
  52. $stmt = $pdo->query("SELECT * FROM books LIMIT 1");
  53. $row = $stmt->fetch();
  54. /**
  55. * we can also fetch() another way:
  56. * fetch(PDO::FETCH_NUM) - every field has a numeric key
  57. * fetch(PDO::FETCH_ASSOC) - every field has the column name as key (default, as set in $opt)
  58. * fetch(PDO::FETCH_BOTH) - both of the above
  59. * fetch(PDO::FETCH_OBJ) - returns object
  60. * fetch(PDO::FETCH_LAZY) - all three
  61. * fetch(PDO::FETCH_CLASS, 'Book') - the returned row is of a certain class (also works on multiple rows)
  62. */
  63. var_dump($row);
  64. echo '<br />';
  65. }
  66. catch (PDOException $e) {
  67. die('There was a problem when trying to read one row from table. Returned message was: '.$e->getMessage());
  68. }
  69.  
  70. // READ ONLY ONE COLUMN FROM A RETURNED ROW --------------------------------------------------------
  71. try {
  72. $stmt = $pdo->prepare("SELECT title FROM books WHERE author LIKE ? LIMIT 1");
  73. $stmt->execute(['Christie Golden']);
  74. $bookTitle = $stmt->fetchColumn();
  75. echo 'Title: '.$bookTitle.'<br />';
  76. }
  77. catch (PDOException $e) {
  78. die('There was a problem when trying to read one row column from table. Returned message was: '.$e->getMessage());
  79. }
  80.  
  81. // READ COUNT OF RESULT-----------------------------------------------------------------------------
  82. try {
  83. $count = $pdo->query("SELECT count(*) FROM books")->fetchColumn();
  84. echo 'Total rows: '.$count.'<br />';
  85. }
  86. catch (PDOException $e) {
  87. die('There was a problem when trying to read one row column from table. Returned message was: '.$e->getMessage());
  88. }
  89.  
  90. // UPDATE ------------------------------------------------------------------------------------------
  91. try {
  92. $sql = "UPDATE books SET collection = ? WHERE collection = ?";
  93. $pdo->prepare($sql)->execute(['World of Warcraft', 'Warcraft']);
  94. }
  95. catch (PDOException $e) {
  96. die('There was a problem when trying to update. Returned message was: '.$e->getMessage());
  97. }
  98.  
  99. // UPDATE WITH ROWS COUNT --------------------------------------------------------------------------
  100. try {
  101. $stmt = $pdo->prepare("UPDATE books SET collection = ? WHERE collection = ?");
  102. $stmt->execute(['Warcraft', 'World of Warcraft']);
  103. $affectedRows = $stmt->rowCount();
  104. echo 'Affected rows: '.$affectedRows.'<br />';
  105. }
  106. catch (PDOException $e) {
  107. die('There was a problem when trying to update. Returned message was: '.$e->getMessage());
  108. }
  109.  
  110. // DELETE ------------------------------------------------------------------------------------------
  111. try {
  112. $sql = "DELETE FROM books WHERE title LIKE ?";
  113. $pdo->prepare($sql)->execute(['%Crimes%']);
  114. }
  115. catch (PDOException $e) {
  116. die('There was a problem when trying to delete. Returned message was: '.$e->getMessage());
  117. }
  118.  
  119. // DELETE WITH ROW COUNT --------------------------------------------------------------------------
  120. try {
  121. $count = $pdo->prepare("DELETE FROM books");
  122. $count->execute();
  123. $deletedRows = $count->rowCount();
  124. echo ('Deleted rows: '.$deletedRows);
  125. }
  126. catch (PDOException $e) {
  127. die('There was a problem when trying to delete. Returned message was: '.$e->getMessage());
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement