March 12, 201214 yr Just started to learn PDO and Prepared Statements, been meaning to do this for a while. Just wondering if the following is technically correct, if it's missing anything or whatever? Are there any good tutorials anyone knows that I can read that I may not have found? <?php try { $user = 'zend'; $pass = ''; $dbh = new PDO('mysql:host=localhost;dbname=Sites', $user, $pass); $name = 'adam'; $stmt = $dbh->prepare("SELECT * FROM User WHERE Username = ?"); $stmt -> setFetchMode(PDO::FETCH_ASSOC); $stmt->execute(array($name)); $row = $stmt->fetch(); ?> <html> <head> </head> <body> <?php echo $row['Email']; echo "<br />"; echo $row['Username']; ?> </body> </html> <?php $dbh = null; } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die(); } ?>
March 13, 201214 yr someone must use prepared statements cant say i use them as most as alot of guys on here but ur code looks fine to me i always do my statements in this mannor though $dbh = new PDO('mysql:host=localhost;dbname=Sites', $user, $pass); $name = 'adam'; $stmt = $dbh->prepare("SELECT * FROM User WHERE Username = :username"); $row = $stmt->fetch(); $stmt->execute(":username",PDO::PARAM_STR); Edited March 13, 201214 yr by webdesigner93
Create an account or sign in to comment