Difference between PDO->query() and PDO->exec()

Regardless of whatever theoretical difference, neither PDO::query() nor PDO::exec() should be used anyway. These functions don’t let you bind parameters to the prepared statement and should never be used. Use prepare()/execute() instead, especially for UPDATE,INSERT,DELETE statements. Please note that although prepared statements are widely advertised as a security measure, it is only to attract people’s … Read more

PDO Exception Questions – How to Catch Them

You should look at the documentation. But If you dont find anything, you can add another catch : <?php try { $stmt = $db->prepare(“INSERT INTO tbl_user (id, name, password, question, answer) VALUES (NULL, :name, :password, :question, :answer)”); $stmt->bindValue(“:name”, $_POST[‘name’]); $stmt->bindValue(“:password”, $_POST[‘password’]); $stmt->bindValue(“:question”, $_POST[‘question’]); $stmt->bindValue(“:answer”, $_POST[‘answer’]); $stmt->execute(); echo “Successfully added the new user ” . $_POST[‘name’]; … Read more

prepared parameterized query with PDO

To create the connection try { $db = new PDO(“mysql:dbname=”.DB_NAME.”;host=”.DB_HOST,DB_USER,DB_PWD); } catch (PDOException $e) { die(“Database Connection Failed: ” . $e->getMessage()); } Then to prepare a statement $prep = $db->prepare(“SELECT * FROM `users` WHERE userid = ‘:id'”); As you can see, you label each parameter you’d like by prefixing any string with ‘:’. Then all … Read more