<bdo id='wMKja'></bdo><ul id='wMKja'></ul>

  1. <tfoot id='wMKja'></tfoot>

    <i id='wMKja'><tr id='wMKja'><dt id='wMKja'><q id='wMKja'><span id='wMKja'><b id='wMKja'><form id='wMKja'><ins id='wMKja'></ins><ul id='wMKja'></ul><sub id='wMKja'></sub></form><legend id='wMKja'></legend><bdo id='wMKja'><pre id='wMKja'><center id='wMKja'></center></pre></bdo></b><th id='wMKja'></th></span></q></dt></tr></i><div id='wMKja'><tfoot id='wMKja'></tfoot><dl id='wMKja'><fieldset id='wMKja'></fieldset></dl></div>
    <legend id='wMKja'><style id='wMKja'><dir id='wMKja'><q id='wMKja'></q></dir></style></legend>
    1. <small id='wMKja'></small><noframes id='wMKja'>

    2. 无法将内爆数组绑定到 mysql 准备好的语句中

      时间:2023-07-29

      <legend id='Pljyy'><style id='Pljyy'><dir id='Pljyy'><q id='Pljyy'></q></dir></style></legend>

          <bdo id='Pljyy'></bdo><ul id='Pljyy'></ul>
          <tfoot id='Pljyy'></tfoot>
          1. <i id='Pljyy'><tr id='Pljyy'><dt id='Pljyy'><q id='Pljyy'><span id='Pljyy'><b id='Pljyy'><form id='Pljyy'><ins id='Pljyy'></ins><ul id='Pljyy'></ul><sub id='Pljyy'></sub></form><legend id='Pljyy'></legend><bdo id='Pljyy'><pre id='Pljyy'><center id='Pljyy'></center></pre></bdo></b><th id='Pljyy'></th></span></q></dt></tr></i><div id='Pljyy'><tfoot id='Pljyy'></tfoot><dl id='Pljyy'><fieldset id='Pljyy'></fieldset></dl></div>
                  <tbody id='Pljyy'></tbody>

              • <small id='Pljyy'></small><noframes id='Pljyy'>

              • 本文介绍了无法将内爆数组绑定到 mysql 准备好的语句中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我对以下语法错误感到头疼.我正在尝试将内爆数组绑定到准备好的语句中,但出现以下语法错误:

                I am beating my head over the below syntax error. I am trying to bind an imploded array into a prepared statement, but I am getting the following syntax error:

                您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在?"附近使用的正确语法.在第 1 行

                这是我的代码.谁能看出我哪里出错了?

                Here is my code. Can anyone see where I am going wrong?

                <?php 
                include('config.php');
                
                $selected = $_POST['selected'];
                
                if ($stmt = $mysqli->prepare("DELETE FROM email_addresses WHERE email_addresses IN ?")) {
                
                    $stmt->bind_param("s", "('" . implode("', '", $selected) . "')" );
                
                    $stmt->execute();
                
                    $stmt->close();
                
                    print "ok";
                
                } else {
                    print $mysqli->error;
                }
                
                $mysqli->close();
                
                ?>
                

                作为测试,我尝试了:

                print "('" . implode("', '", $selected) . "')";
                

                哪个正确地给了我

                ('me@me.com', 'you@you.com')
                

                推荐答案

                让我为您省去一些麻烦并告诉您您尝试做的事情无论如何都行不通.您只将一个参数绑定到您的 IN() 函数调用.您认为您正在传递一个逗号分隔的列表,但实际上您只传递了一个逗号分隔的字符串,该字符串被视为一个值.这意味着您将搜索具有'me@me.com'、'you@you.com'"值的一条记录,而不是与me@me.com"匹配的记录或you@you.com".

                Let me save you some trouble and tell you what you're trying to do won't work anyway. You are only binding one parameter to your IN() function call. You think you're passing a comma separated list but you are actually only passing a comma separated string which is treated as one value. This means you will be search for one record with a value of "'me@me.com', 'you@you.com'" instead of records that match "me@me.com" or "you@you.com".

                要克服这个问题,您需要:

                To overcome this you need to:

                1. 动态生成类型字符串
                2. 使用call_user_func_array()绑定你的参数

                您可以像这样生成类型字符串:

                You can generate the types string like this:

                $types = str_repeat('s', count($selected));
                

                所有这些都是创建一个 s 的字符串,它的字符数与数组中的元素数一样多.

                All this does is create a string of s's that is as many characters as the number of elements in the array.

                然后你可以像这样使用 call_user_func_array() 绑定你的参数(注意我把括号放回 IN() 函数):

                You would then bind your parameters using call_user_func_array() like this (notice I put the parenthesis back in for the IN() function):

                if ($stmt = $mysqli->prepare("DELETE FROM email_addresses WHERE email_addresses IN (?)")) {
                    call_user_func_array(array($stmt, "bind_param"), array_merge($types, $selected));
                

                但是如果你尝试这个你会得到一个关于 mysqli_stmt::bind_param() 期望参数二通过引用传递的错误:

                But if you try this you will get an error about mysqli_stmt::bind_param() expecting parameter two to be passed by reference:

                警告:mysqli_stmt::bind_param() 的参数 2 应为引用,已给定值

                Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given

                这有点烦人,但很容易解决.要解决此问题,您可以使用以下函数:

                This is kind of annoying but easy enough to work around. To work around that you can use the following function:

                function refValues($arr){ 
                    $refs = array(); 
                    foreach($arr as $key => $value) 
                        $refs[$key] = &$arr[$key]; 
                    return $refs; 
                } 
                

                它只是创建一个值数组,这些值是对 $selected 数组中值的引用.这足以让 mysqli_stmt::bind_param() 开心:

                It just creates an array of values that are references to the values in the $selected array. This is enough to make mysqli_stmt::bind_param() happy:

                if ($stmt = $mysqli->prepare("DELETE FROM email_addresses WHERE email_addresses IN (?)")) {
                    call_user_func_array(array($stmt, "bind_param"), array_merge($types, refValues($selected)));
                

                编辑

                从 PHP 5.6 开始,您现在可以使用 ... 运算符来使这更简单:

                As of PHP 5.6 you can now use the ... operator to make this even simpler:

                $stmt->bind_param($types, ...$selected);
                

                这篇关于无法将内爆数组绑定到 mysql 准备好的语句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                <i id='zrQro'><tr id='zrQro'><dt id='zrQro'><q id='zrQro'><span id='zrQro'><b id='zrQro'><form id='zrQro'><ins id='zrQro'></ins><ul id='zrQro'></ul><sub id='zrQro'></sub></form><legend id='zrQro'></legend><bdo id='zrQro'><pre id='zrQro'><center id='zrQro'></center></pre></bdo></b><th id='zrQro'></th></span></q></dt></tr></i><div id='zrQro'><tfoot id='zrQro'></tfoot><dl id='zrQro'><fieldset id='zrQro'></fieldset></dl></div>

                <tfoot id='zrQro'></tfoot>

                    <tbody id='zrQro'></tbody>

                1. <legend id='zrQro'><style id='zrQro'><dir id='zrQro'><q id='zrQro'></q></dir></style></legend>
                    • <bdo id='zrQro'></bdo><ul id='zrQro'></ul>

                      <small id='zrQro'></small><noframes id='zrQro'>