PDO Prepared Statements


/ Published in: PHP
Save to your folder(s)

Using Prepared Statements.


Copy this code and paste it in your HTML
  1. /*
  2.  * The Prepared Statements Method
  3.  * Best Practice
  4.  */
  5.  
  6. $id = 5;
  7. try {
  8. $conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
  9. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10.  
  11. $stmt = $conn->prepare('SELECT * FROM myTable WHERE id = :id');
  12. $stmt->execute(array('id' => $id));
  13.  
  14. while($row = $stmt->fetch()) {
  15. print_r($row);
  16. }
  17. } catch(PDOException $e) {
  18. echo 'ERROR: ' . $e->getMessage();
  19. }

URL: http://net.tutsplus.com/tutorials/php/php-database-access-are-you-doing-it-correctly/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.