PHP MySQL PDO DELETE with prepared statement and named placeholders
From w3cyberlearnings
Contents |
PHP PDO Delete
Use name place holder and bind parameter to delete a specific record.
Syntax PDO Delete
sql = "DELETE FROM user_infor WHERE id=:id"; $sq = $db->prepare($sql); $sq->bindParam('id', 1, PDO::PARAM_INT);
Example 1
<?php $delete_array = array( array('id' => 1), array('id' => 2), array('id' => 3) ); // fetch as NUM $dns = 'mysql:host=localhost;dbname=w3cyberlearning'; $user = 'user2000'; $pass = 'password2000'; $db = new PDO($dns, $user, $pass); // delete records $sql = "DELETE FROM user_infor WHERE id=:id"; $sq = $db->prepare($sql); // key($id); to get array key foreach ($delete_array as $id) { $sq->bindParam(key($id), $id[key($id)], PDO::PARAM_INT); $sq->execute(); echo $sq->rowCount(); echo "<br/>"; } ?>