Jump to: navigation, search

PHP MySQL PDO DELETE with prepared statement and question mark placeholders

From w3cyberlearnings

Contents

PHP PDO Delete Record

Use question mark place holder and bind parameter to delete a specific record.

Syntax PDO Delete Record

$id=1;
sql = "DELETE FROM user_infor WHERE id=?";

$sq = $db->prepare($sql);
$sq->bindParam(1, $id, PDO::PARAM_INT);
$sq->execute();

Example 1

<?php

$delete_array = array(
	 1, 2, 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=?";

$sq = $db->prepare($sql);

foreach ($delete_array as $id) {
	$sq->bindParam(1, $id, PDO::PARAM_INT);
	$sq->execute();
	echo $sq->rowCount();
	echo "<br/>";
}

?>


Related Links


Navigation
Web
SQL
MISC
References