Php mysqli到PDO的翻译

Php mysqli到PDO的翻译,php,mysql,database,pdo,mysqli,Php,Mysql,Database,Pdo,Mysqli,下面的代码对我来说至少是直截了当的。我想用PDO实现同样的目标。然而,尽管我尽了最大努力,我还是无法理解这个概念。有人能解释一下吗 //Connect to a database. $link = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die("Couldn't connect to database."); //Delete from the multiple tables. $sql = "DELETE FROM t

下面的代码对我来说至少是直截了当的。我想用PDO实现同样的目标。然而,尽管我尽了最大努力,我还是无法理解这个概念。有人能解释一下吗

//Connect to a database.
$link  = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die("Couldn't connect to database.");

//Delete from the multiple tables. 
$sql = "DELETE FROM table1, table2, tables3, tables4 WHERE id='75'";
$result = mysqli_query($link , $sql);

不能在一个查询中删除多个表。请尝试在PDO中使用foreach

$pdo = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
$tables = array("table1","table2","table3","table4");
foreach($tables as $table) {
  $sql = "DELETE FROM $table WHERE id = :id";
  $stmt = $pdo->prepare($sql);
  $stmt->bindParam(':id', $id);  // $id or '75'
  $stmt->execute();
}

在这里,使用准备好的语句仅显示为id=75并不是用户输入,但这是更好的方法,并且使用事务,以防您希望一次删除/更新/插入更多数据,这会更快

$id = 75;


try {
    $pdo = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME , DB_USER, DB_PASS);




$pdo->beginTransaction();

$st = $pdo->prepare('DELETE FROM table1 WHERE id = :id');
$st->execute(array(':id', $id));
$st = $pdo->prepare('DELETE FROM table2 WHERE id = :id');
$st->execute(array(':id', $id));
$st = $pdo->prepare('DELETE FROM table3 WHERE id = :id');
$st->execute(array(':id', $id));
$st = $pdo->prepare('DELETE FROM table4 WHERE id = :id');
$st->execute(array(':id', $id));

$pdo->commit();
    }
catch (PDOException $e) {

    die('Error!: ' . $e->getMessage() . '<br/>');

}

非常感谢,我现在明白多了。速记绝对是最好的。我一直喜欢经济代码。非常感谢。无法进行多次删除的包装器。这就是进步?
$array = array('table1','table2','table3','table4');
foreach ($array as $table) {
$st = $pdo->prepare('DELETE FROM '.$table.' WHERE id = :id');
    $st->execute(array(':id', $id));
}