【发布时间】:2014-11-16 17:19:09
【问题描述】:
我不知道如何使用 PDO 更新或插入多行。请帮帮我。
我的想法是:
$stmt = $dbh->query("update_line_1; update_line_2; update_line_3");
//update_line_1: update table a set a.column1 = "s1" where a.id = 1
//update_line_2: update table a set a.column1 = "s2" where a.id = 2
//....
$stm = $dbh->query("insert_line_1; insert_line_3; insert_line_3");
//something is like the update line above.
我不知道这种方式是否有效。如果您有其他方法,请告诉我。非常感谢。
如果我使用 prepare 语句,我每次只更新每一行。 (这比上面的安全多了)
$stmt = $dbh->prepare("update table a set a.colum1 = :column1 where a.id = :id");
$stmt->bindParam(":column1","s1");
$stmt->bindparam(":id",1);
$stmt->execute();
我最不想做的事情是使用循环遍历数组中的所有元素,并且每次都更新或插入每个元素
是否有另一种方法可以安全地批量更新或向数据库插入多行?感谢您的帮助。
对不起我的英语。
【问题讨论】: