Posted By

Sadeveloper on 12/29/13


Tagged

statements pdo prepared


Versions (?)

PDO Prepared Statements


 / Published in: PHP
 

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

Using Prepared Statements.

  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. }

Report this snippet  

You need to login to post a comment.