Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. <?php
  2. $host = 'localhost';
  3. $user = 'root';
  4. $password = '123456';
  5. $dbname = 'pdoposts';
  6.  
  7. // Set DSN
  8. $dsn = 'mysql:host='. $host .';dbname='. $dbname;
  9.  
  10. // Create a PDO instance
  11. $pdo = new PDO($dsn, $user, $password);
  12. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
  13. $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  14.  
  15. # PRDO QUERY
  16.  
  17. // $stmt = $pdo->query('SELECT * FROM posts');
  18.  
  19. // while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  20. // echo $row['title'] . '<br>';
  21. // }
  22.  
  23. // while($row = $stmt->fetch()){
  24. // echo $row->title . '<br>';
  25. // }
  26.  
  27. # PREPARED STATEMENTS (prepare & execute)
  28.  
  29. // UNSAFE
  30. //$sql = "SELECT * FROM posts WHERE author = '$author'";
  31.  
  32. // FETCH MULTIPLE POSTS
  33.  
  34. // User Input
  35. $author = 'Brad';
  36. $is_published = true;
  37. $id = 1;
  38. $limit = 1;
  39.  
  40. // Positional Params
  41. $sql = 'SELECT * FROM posts WHERE author = ? && is_published = ? LIMIT ?';
  42. $stmt = $pdo->prepare($sql);
  43. $stmt->execute([$author, $is_published, $limit]);
  44. $posts = $stmt->fetchAll();
  45.  
  46. // Named Params
  47. // $sql = 'SELECT * FROM posts WHERE author = :author && is_published = :is_published';
  48. // $stmt = $pdo->prepare($sql);
  49. // $stmt->execute(['author' => $author, 'is_published' => $is_published]);
  50. // $posts = $stmt->fetchAll();
  51.  
  52. // //var_dump($posts);
  53. foreach($posts as $post){
  54. echo $post->title . '<br>';
  55. }
  56.  
  57. // FETCH SINGLE POST
  58.  
  59. // $sql = 'SELECT * FROM posts WHERE id = :id';
  60. // $stmt = $pdo->prepare($sql);
  61. // $stmt->execute(['id' => $id]);
  62. // $post = $stmt->fetch();
  63.  
  64. // echo $post->body;
  65.  
  66. // GET ROW COUNT
  67. // $stmt = $pdo->prepare('SELECT * FROM POSTS WHERE author = ?');
  68. // $stmt->execute([$author]);
  69. // $postCount = $stmt->rowCount();
  70.  
  71. // echo $postCount;
  72.  
  73. // INSERT DATA
  74. // $title = 'Post Five';
  75. // $body = 'This is post five';
  76. // $author = 'Kevin';
  77.  
  78. // $sql = 'INSERT INTO posts(title, body, author) VALUES(:title, :body, :author)';
  79. // $stmt = $pdo->prepare($sql);
  80. // $stmt->execute(['title' => $title, 'body' => $body, 'author' => $author]);
  81. // echo 'Post Added';
  82.  
  83. // UPDATE DATA
  84. // $id = 1;
  85. // $body = 'This is the updated post';
  86.  
  87. // $sql = 'UPDATE posts SET body = :body WHERE id = :id';
  88. // $stmt = $pdo->prepare($sql);
  89. // $stmt->execute(['body' => $body, 'id' => $id]);
  90. // echo 'Post Updated';
  91.  
  92. // DELETE DATA
  93. // $id = 3;
  94.  
  95. // $sql = 'DELETE FROM posts WHERE id = :id';
  96. // $stmt = $pdo->prepare($sql);
  97. // $stmt->execute(['id' => $id]);
  98. // echo 'Post Deleted';
  99.  
  100. // SEARCH DATA
  101. // $search = "%f%";
  102. // $sql = 'SELECT * FROM posts WHERE title LIKE ?';
  103. // $stmt = $pdo->prepare($sql);
  104. // $stmt->execute([$search]);
  105. // $posts = $stmt->fetchAll();
  106.  
  107. // foreach($posts as $post){
  108. // echo $post->title . '<br>';
  109. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement