doukuang8166 2017-07-22 14:59
浏览 215
已采纳

动态生成要在PHP预准备语句的bind_param()函数中使用的变量名

I was wondering if it's possible to store the variables to be inserted in a bind_param() function inside another variable such that if this were the original query:

$stmt->bind_param("si", $column1, $column2);

would it be possible to store the 2 variables inside another variable, i.e:

$columns = $column1.','.$column2;

And then if the new variable can be used inside the bind_param() function like this such that it works:

$stmt->bind_param("si", $columns);

My Database column names and php variable names are the same (except for the '$' sign of course), so I tried this:

$string = "column1, column2";

function convert_to_variables($string) {

        // Convert column names to an array
        $array = explode(", ", $string); 

        // Convert column names array values to a variable
        foreach ($array as $key => $value) {
            $array_values_with_dollar[] = $$value; // (previously tried with '$'.$value)
        }

        // Convert to string again
        $imploded = implode(", ", $array_values_with_dollar); 

        return $imploded;
}

.
.
$variables = convert_to_variables($string);
.
.
$stmt->bind_param("si", $variables );
.
.
// Everything else like $stmt = $dbc->prepare(""); and $stmt->execute(); etc exists. 
// The bind_param() is the only line with an error.

(Right now I'm getting an 'Undefined variable: column1' error.)

  • 写回答

2条回答 默认 最新

  • duandange7480 2017-07-22 15:07
    关注

    It's not possible to do with strings directly, because it'd be interpreted as just that - a single string, and not a combination of strings that'd make up the values for each column. Using that logic, you'd also have issues if one of your strings actually contains ', 'as well - then it wouldn't match up with number of values and number of parameters.

    I can suggest an alternative using arrays instead. You can use that string as a base if you really need to, and use explode() on that. In any case, using an array, you can use the "unpacking operator" ... to unpack the array.

    $string = "column1, column2";
    $boom = explode(", ", $string);
    $stmt->bind_param("ss", ...$boom);
    

    Alternatively build it as an array, and use that (which effectively is the same). This approach is better, as it will not produce more values than parameters if one or more of the strings contains a comma ,.

    $variables = array("column1", "column2");
    $stmt->bind_param("ss", ...$variables);
    

    Also note that I've changed "types" parameter to ss (was si), because both your parameters are in fact strings.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值