php 在 pdo 中绑定多个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12344741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:23:35  来源:igfitidea点击:

Binding multiple values in pdo

phppdobind

提问by Yousuf Memon

Is there's an easy way of binding multiple values in PDO without repitition ? Take a look at the following code :

是否有一种简单的方法可以在不重复的情况下在 PDO 中绑定多个值?看看下面的代码:

$result_set = $pdo->prepare("INSERT INTO `users` (`username`, `password`, `first_name`, `last_name`) VALUES (:username, :password, :first_name, :last_name)");

$result_set->bindValue(':username', '~user');
$result_set->bindValue(':password', '~pass');
$result_set->bindValue(':first_name', '~John');
$result_set->bindValue(':last_name', '~Doe');

$result_set->execute();

Here, I binded values in a repepeated way which is 4 times. So is there's an easy way of binding multiple values in PDO ?

在这里,我以 4 次重复的方式绑定值。那么是否有一种简单的方法可以在 PDO 中绑定多个值?

回答by jeremy

You can always bind values within the arguments of execute()as long as you're fine with the values being treated as PDO::PARAM_STR(string).

execute()只要您对被视为PDO::PARAM_STR(字符串)的值感到满意,您始终可以在参数中绑定值。

$result_set = $pdo->prepare("INSERT INTO `users` (`username`, `password`, `first_name`, `last_name`) VALUES (:username, :password, :first_name, :last_name)");
$result_set->execute(array(
    ':username' => '~user',
    ':password' => '~pass',
    ':first_name' => '~John',
    ':last_name' => '~Doe'
));

You can use the array passed just like any array:

您可以像使用任何数组一样使用传递的数组:

$user = "Nile";
$pdo->execute(array(":user" => $user));

回答by Corbin

If you want to bind based on type (string, int, etc), then no. If you're fine with binding everything as a string:

如果您想根据类型(字符串、整数等)进行绑定,则不可以。如果您可以将所有内容绑定为字符串:

$stmt = $db->prepare("...");
$stmt->execute(array(
    'foo' => 'bar',
    'something' => 'else',
    'third' => 'thing',
));

回答by equazcion

To truly never type anything twice, you can use an array to supply the data, and use a function on that same array to output the binding portion of the MySQL query. For example:

要真正永远不要两次输入任何内容,您可以使用一个数组来提供数据,并在同一个数组上使用一个函数来输出 MySQL 查询的绑定部分。例如:

function bindFields($fields){
    end($fields); $lastField = key($fields);
    $bindString = ' ';
    foreach($fields as $field => $data){ 
        $bindString .= $field . '=:' . $field; 
        $bindString .= ($field === $lastField ? ' ' : ',');
    }
    return $bindString;
}

The data and column names come from a single associative array ($data). Then, use bindFields($data)to generate a string of column = :columnpairs to concatenate into the MySQL query:

数据和列名来自单个关联数组 ( $data)。然后,使用bindFields($data)生成一column = :column对字符串连接到 MySQL 查询中:

$data = array(
    'a_column_name' => 'column data string',
    'another_column_name' => 'another column data string'
);

$query = "INSERT INTO tablename SET" . bindFields($data);

$result = $PDO->prepare($query);
$result->execute($data);

bindFields($data)output:

bindFields($data)输出:

 a_column_name=:a_column_name,another_column_name=:another_column_name