【问题标题】:Determine number of variables and automatically bind_param with correct number of variables确定变量数量并使用正确数量的变量自动绑定参数
【发布时间】:2017-03-20 14:30:10
【问题描述】:

在底部我有一个函数,它接受一个 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;
}

提前谢谢你。

【问题讨论】:

  • 如果这是 MySQLi,那么 bind_param() 将通过 splat 运算符接受多个参数:$MySQLquery->bind_param($query['paramTypes'], ...$query['params']);
  • 嗨,马克,感谢您的回复。它是 MySQLi。我知道我可以给它多个变量;我的问题是我将这些变量传递给它的方式以及我通过检查数组中元素的数量这样做的效率。
  • PHP 5.6 引入了argument packing/unpacking aka the splat operator
  • @MarkBaker 我可能忽略了一些东西,但这仍然不能解决我的问题。我无法将数组传递给 bind_param。如果我应该能够,它只是不让我。当我尝试将 $query['params'] 直接传递给 bind_param 时,出现错误:mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables
  • 如果您使用 splat 运算符,则不会直接传递 $query['params']:使用 ...$query['params'],您将解包 $query['params'] 以传递数组的各个元素

标签: php mysql arrays mysqli prepared-statement


【解决方案1】:

使用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']相符

【讨论】:

  • 谢谢马克!非常感谢您对此提供的帮助!
猜你喜欢
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-25
  • 2014-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多