gpt4 book ai didi

php - 将所有参数作为数组传递 PHP mysqli 查询?

转载 作者:行者123 更新时间:2023-11-29 01:05:21 26 4
gpt4 key购买 nike

我想传递数组中的所有参数,而不是一个一个地传递它们。

我的查询:

Select col_1,col_2,col_3,col_4 FROM table WHERE col_1=?i AND col_2 =?s AND col_3=?i AND col_4=?s;

$list = $db_con->getAll($sql, 12,'string',18,'name'); //Currently I've passed

是否可以像下面这样传递?

$list = $db_con->getAll($sql, array(12,'string',18,'name')); //Want to do

提前致谢。

最佳答案

从 PHP 5.6 开始,您可以使用 splat operator :

$args = array(12,'string',18,'name');
$list = $db_con->getAll($sql, ...$args);

如果您的 PHP 版本不是最新的,那么 call_user_func_array() 可以这样使用:

$sql = "Select col_1,col_2,col_3,col_4 FROM tabele WHERE col_1=?i AND col_2 =?s AND col_3=?i AND col_4=?s";
$args = array(12,'string',18,'name');
array_unshift($args, $sql);
$list = call_user_func_array(array($db_con, 'getAll'), $args);

但如您所见,第一个版本要好得多,更不用说所有低于 5.6 的 PHP 版本都已经过时了 - 所以我强烈建议您升级。

关于php - 将所有参数作为数组传递 PHP mysqli 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38842401/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com