使用单个参数并使用 bindParam 插入大量记录
Insert a lot of record using single arguments and using bindParam
我有一些方法可以像这样将一些数据插入数据库:
public function register($username, $email, $hashedPassword, $activationCode)
{
try {
$conn = Database::getConnection();
// Connect and create the PDO object
$conn->exec('SET CHARACTER SET utf8'); // Sets encoding UTF-8
// Define and prepare an INSERT statement
$sql = 'INSERT INTO users (username, email, pass, reset_token, dateAdded )
VALUES (:username, :pass, :email, :token, now())';
$sqlprep = $conn->prepare($sql);
// Adds value with bindParam
$sqlprep->bindParam(':username', $username, PDO::PARAM_STR);
$sqlprep->bindParam(':email', $email, PDO::PARAM_STR);
$sqlprep->bindParam(':pass', $hashedPassword);
$sqlprep->bindParam(':token', $activationCode);
// If the query is successfully executed, output the value of the last insert id
if ($sqlprep->execute()) {
//echo 'Succesfully added the row with id='. $conn->lastInsertId();
$this->result = true;
}
$conn = null; // Disconnect
} catch (PDOException $e) {
include('../views/error.php');
include('../views/admin/includes/footer.php');
exit();
}
}
问题是,如果我的函数要输入数据库的参数太多,我认为这不是一个好方法。那么我可以仅通过使用 1 个参数但仍然使用 bindParam 来输入很多字段,这是什么好方法吗?因为我看到很多例子只使用没有 bindParam 的准备。我想我可以使用数组,但我不知道正确的方法。所以我需要一些帮助。
您可以将参数作为数组插入 $sqlprep->execute($param_array)
或者,简单地将每个参数传递到执行中的数组中,如下所示:$sqlprep->execute(array($param1, $param2))
更新:
将值作为数组传递到 $input 中:
$input = array('username' => $username, 'activationHash' => $activationHash); //and so on
现在在模型方面,
您可以像这样使用 foreach 循环将这些值绑定到参数:
foreach ($values as $key => $value) {
$sqlprep->bindParam(':' . $key, $value , PDO::PARAM_STR);
}
检查第二个示例,您必须使用 binds
重复值
例如
值 (:username1, :pass1, :email1, :token1, now()), (:username2, :pass2, :email2, :token2, now())
和带循环的 bindParam
既然你想保留你的绑定参数,我建议你使用这样的输入:
$input = array('username' => $username, 'activationHash' => $activationHash);
并在您的 bindParam 中添加如下代码:
public function register($input){
//code
$sqlprep->bindParam(':username', $input['username'], PDO::PARAM_STR);
//other
}
希望这能解决您的问题
我有一些方法可以像这样将一些数据插入数据库:
public function register($username, $email, $hashedPassword, $activationCode)
{
try {
$conn = Database::getConnection();
// Connect and create the PDO object
$conn->exec('SET CHARACTER SET utf8'); // Sets encoding UTF-8
// Define and prepare an INSERT statement
$sql = 'INSERT INTO users (username, email, pass, reset_token, dateAdded )
VALUES (:username, :pass, :email, :token, now())';
$sqlprep = $conn->prepare($sql);
// Adds value with bindParam
$sqlprep->bindParam(':username', $username, PDO::PARAM_STR);
$sqlprep->bindParam(':email', $email, PDO::PARAM_STR);
$sqlprep->bindParam(':pass', $hashedPassword);
$sqlprep->bindParam(':token', $activationCode);
// If the query is successfully executed, output the value of the last insert id
if ($sqlprep->execute()) {
//echo 'Succesfully added the row with id='. $conn->lastInsertId();
$this->result = true;
}
$conn = null; // Disconnect
} catch (PDOException $e) {
include('../views/error.php');
include('../views/admin/includes/footer.php');
exit();
}
}
问题是,如果我的函数要输入数据库的参数太多,我认为这不是一个好方法。那么我可以仅通过使用 1 个参数但仍然使用 bindParam 来输入很多字段,这是什么好方法吗?因为我看到很多例子只使用没有 bindParam 的准备。我想我可以使用数组,但我不知道正确的方法。所以我需要一些帮助。
您可以将参数作为数组插入 $sqlprep->execute($param_array)
或者,简单地将每个参数传递到执行中的数组中,如下所示:$sqlprep->execute(array($param1, $param2))
更新:
将值作为数组传递到 $input 中:
$input = array('username' => $username, 'activationHash' => $activationHash); //and so on
现在在模型方面, 您可以像这样使用 foreach 循环将这些值绑定到参数:
foreach ($values as $key => $value) {
$sqlprep->bindParam(':' . $key, $value , PDO::PARAM_STR);
}
检查第二个示例,您必须使用 binds
重复值
例如
值 (:username1, :pass1, :email1, :token1, now()), (:username2, :pass2, :email2, :token2, now())
和带循环的 bindParam
既然你想保留你的绑定参数,我建议你使用这样的输入:
$input = array('username' => $username, 'activationHash' => $activationHash);
并在您的 bindParam 中添加如下代码:
public function register($input){
//code
$sqlprep->bindParam(':username', $input['username'], PDO::PARAM_STR);
//other
}
希望这能解决您的问题