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

Trouble binding an imploded array into a mysql prepared statement(无法将内爆数组绑定到 mysql 准备好的语句中)
本文介绍了无法将内爆数组绑定到 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 准备好的语句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)