Return to Snippet

Revision: 65601
at December 29, 2013 00:24 by Sadeveloper


Initial Code
/*
 * The Prepared Statements Method
 * Best Practice
 */

$id = 5;
try {
    $conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
    
    $stmt = $conn->prepare('SELECT * FROM myTable WHERE id = :id');
    $stmt->execute(array('id' => $id));

    while($row = $stmt->fetch()) {
        print_r($row);
    }
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

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

Initial Description
Using Prepared Statements.

Initial Title
PDO Prepared Statements

Initial Tags


Initial Language
PHP