danny1995

php pdo

Jun 12th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.58 KB | None | 0 0
  1. <?php
  2.  
  3. $host="localhost";
  4. $user="root";
  5. $password="";
  6. $dbname="pdopost";
  7.  
  8. //Set DSN
  9. $dsn='mysql:host='.$host.';dbname='.$dbname;
  10.  
  11. //Create a PDO instance
  12. $pdo=new PDO($dsn,$user,$password);
  13. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_OBJ);
  14. $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
  15.  
  16. #PDO QUERY
  17.  
  18. //$stmt=$pdo->query('SELECT *from posts');
  19.  
  20. // // while($row=$stmt->fetch(PDO::FETCH_ASSOC))
  21. // // {
  22. // //     echo $row['title'].'<br>';
  23. // // }
  24.  
  25. // while($row=$stmt->fetch())
  26. // {
  27. //     echo json_encode($row);
  28. //     //echo $row.'<br>';
  29. // }
  30.  
  31. #PREPARED STATEMENTS (prepare & execute)
  32.  
  33. //UNSAFE
  34.  
  35. // $auther='grave keeper';
  36. // $id=1;
  37. // $limit=1;
  38.  
  39. //Positional Params
  40. // $sql='SELECT *From posts WHERE auther=? LIMIT ?'; //LIMIT to one
  41. // $sql='SELECT *From posts WHERE auther=?';
  42. // //$sql='SELECT *From posts WHERE auther=? AND id=? ';
  43. // //$sql='SELECT *From posts WHERE auther=? OR id=? ';
  44. // $stmt=$pdo->prepare($sql);
  45. // $stmt->execute([$auther],[$limit]);
  46. // $stmt->execute([$auther]);
  47. // //$stmt->execute([$auther,$id]);
  48. // $posts=$stmt->fetchAll();
  49.  
  50. // foreach($posts as $post)
  51. // {
  52. // echo json_encode($post);
  53. // }
  54.  
  55. //Named Params
  56. // $sql='SELECT *From posts WHERE auther=:auther';
  57. // //$sql='SELECT *From posts WHERE auther=:auther AND id=:id ';
  58. // //$sql='SELECT *From posts WHERE auther=:auther OR id=:id ';
  59. // $stmt=$pdo->prepare($sql);
  60. // $stmt->execute(['auther'=>$auther]);
  61. // //$stmt->execute([$auther,$id]);
  62. // $posts=$stmt->fetchAll();
  63. // //$posts=$stmt->fetch(); --> one item only
  64.  
  65. // foreach($posts as $post)
  66. // {
  67. // echo json_encode($post);
  68. // }
  69.  
  70. // echo  '<br>'.$stmt->rowCount();
  71.  
  72. //INSERT DATA
  73. // $title='post 4';
  74. // $body='grave keeper post 4';
  75. // $auther='grave keeper';
  76.  
  77. // $sql='INSERT INTO posts(title,body,auther) VALUES(:title,:body,:auther)';
  78. // $stmt=$pdo->prepare($sql);
  79. // $stmt->execute(['title'=>$title,'body'=>$body,'auther'=>$auther]);
  80. // echo "New post added";
  81.  
  82. //UPDATE DATA
  83. // $body='grave keeper updated post 1';
  84. // $id=1;
  85.  
  86. // $sql='UPDATE posts SET body=:body WHERE id=:id';
  87. // $stmt=$pdo->prepare($sql);
  88. // $stmt->execute(['body'=>$body,'id'=>$id]);
  89. // echo "post updated";
  90.  
  91. // //DELETE DATA
  92. // $id=3;
  93.  
  94. // $sql='DELETE FROM posts WHERE id=:id';
  95. // $stmt=$pdo->prepare($sql);
  96. // $stmt->execute(['id'=>$id]);
  97. // echo "post deleted";
  98.  
  99. //SEARCH DATA
  100. $search='%keeper%';
  101.  
  102. $sql='SELECT * FROM posts WHERE auther LIKE ?';
  103. $stmt=$pdo->prepare($sql);
  104. $stmt->execute([$search]);
  105. $posts=$stmt->fetchAll();
  106.  
  107. foreach($posts as $post)
  108. {
  109. echo json_encode($post);
  110. }
  111.  
  112. ?>
Add Comment
Please, Sign In to add comment