php - 确定变量的数量并自动使用正确数量的变量进行bind_param

标签 php mysql arrays mysqli prepared-statement

在底部,我有一个函数,它接受 MySQL 查询数组并自动准备语句。所以我可以设置一个如下所示的数组:

$queryTest = array(
    array(
        'query' => 'SELECT * FROM tools WHERE RFID_tag = ? AND editedByID = ?',
        'paramTypes' => 'si',
        'params' => array('EXAMPLE_RFID_TAG', 1001)
    ),
    array(
        'query' => 'SELECT * FROM tool_categories WHERE categoryID = ?',
        'paramTypes' => 'i',
        'params' => array(1)
    )
);

然后调用$resultArray = db_multi_query($queryTest);,您可以按照您认为合适的方式处理它。

我的问题是:是否有更好的方法来确定数组中的语句变量数量,以自动填充 bind_param 变量,而不是通过使用 检查变量数量来实现if(count($query['params']) == 5){ 然后手动添加更多变量到bind_param?我希望它自动用变量填充bind_param,这样我就可以缩短这个函数,这样我就不受 if(count($query['params']) == int){ 的数量限制 我在函数中设置。

function db_multi_query(array $queries){
    global $MySQLi;
    $resultArray = array();
    foreach($queries as $query){
        $MySQLquery = $MySQLi->prepare($query['query']);
        if(count($query['params']) == 1){
            $MySQLquery->bind_param($query['paramTypes'], $query['params'][0]);
        }
        if(count($query['params']) == 2){
            $MySQLquery->bind_param($query['paramTypes'], $query['params'][0], $query['params'][1]);
        }
        if(count($query['params']) == 3){
            $MySQLquery->bind_param($query['paramTypes'], $query['params'][0], $query['params'][1], $query['params'][2]);
        }
        if(count($query['params']) == 4){
            $MySQLquery->bind_param($query['paramTypes'], $query['params'][0], $query['params'][1], $query['params'][2], $query['params'][3]);
        }
        if(count($query['params']) == 5){
            $MySQLquery->bind_param($query['paramTypes'], $query['params'][0], $query['params'][1], $query['params'][2], $query['params'][3], $query['params'][4]);
        }
        $MySQLquery->execute();
        $resultArray[] = $MySQLquery->get_result();
    }
    return $resultArray;
}

提前谢谢您。

最佳答案

使用Array Unpacking ,又名 PHP 5.6.0 中引入的“splat”运算符(正式名称为“Variadics”),您可以将“params”数组解压到 bind_param() 调用中,因此替换

if(count($query['params']) == 1){
    $MySQLquery->bind_param($query['paramTypes'], $query['params'][0]);
}
if(count($query['params']) == 2){
    $MySQLquery->bind_param($query['paramTypes'], $query['params'][0], $query['params'][1]);
}
if(count($query['params']) == 3){
    $MySQLquery->bind_param($query['paramTypes'], $query['params'][0], $query['params'][1], $query['params'][2]);
}
if(count($query['params']) == 4){
    $MySQLquery->bind_param($query['paramTypes'], $query['params'][0], $query['params'][1], $query['params'][2], $query['params'][3]);
}
if(count($query['params']) == 5){
    $MySQLquery->bind_param($query['paramTypes'], $query['params'][0], $query['params'][1], $query['params'][2], $query['params'][3], $query['params'][4]);
}

if(count($query['params']) > 1) {
    $MySQLquery->bind_param($query['paramTypes'], ...$query['params']);
}

然后,只要参数数量与您的 $query['paramTypes'] 相符,传递多少参数就无关紧要了

关于php - 确定变量的数量并自动使用正确数量的变量进行bind_param,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42906242/

相关文章:

php - 仅选择当前日期或 future 日期的所有内容

php - PDO fetchAll 数组到一维

php - whereHas 之后的查询在 Laravel 5.4 中不起作用

javascript - Ajax 推进

MySQL - 无法查询现有表

javascript - 我的 HTML 表单不会返回数组中的值?

php - 如何在php中删除数组键名称中的空格?

php - session 哈希大小重要吗?

php - 如何有效地将两个(或更多)关联数组与公共(public)键组合起来

Mysql表: Insert a column with ascending numbers (1 to [total number of rows]) in the order of the datetime column