我正在尝试将数组内容添加到数据库中.因为它我正在使用
$arr = ["10","11","12","13"];
foreach ($arr as $value) {
$sql = "INSERT INTO `test` (user_id, view) VALUES (:user_id, :view)";
$stmt = $this->conn->prepare($sql);
$stmt->bindParam(':user_id', $value);
$stmt->bindParam(':view', 'small');
$stmt->execute();
}
Run Code Online (Sandbox Code Playgroud)
是好的做法还是坏的?还有其他好方法吗?
适当的例子是这样的:
$arr = ["10","11","12","13"];
$sql = "INSERT INTO `test` (`user_id`, `view`) VALUES (:user_id, :view)";
$stmt = $this->conn->prepare($sql);
foreach ($arr as $value) {
$stmt->execute(array('user_id' => $value, 'view' => 'small'));
}
Run Code Online (Sandbox Code Playgroud)