dongyuan1870 2014-06-26 07:39
浏览 48

MYSQLi中的准备语句与“SELECT WHERE IN”结合使用

I'm facing a little problem with mysqli and prepared statements:

I want to prepare and "IN" statement in sqli but it does not work for me! :-(

Can someone help or explain me what I'm doing wrong? I think it's because of the way prepared statements work and therefore like some magic (strip, addslashed ...) are preventing my solution so maybe I need something else but can figure it out. This is my sql statement:

$dbPrepare = $db->prepare(
    'SELECT
    `name`, `image`
    FROM `sometable`
    WHERE `number` IN (?)'
);
$dbPrepare->bind_param('s', $numbers);

and that is my way of achieving the "$numbers":

$numbers = implode('","', explode(',', $_GET['numbers']));

the "var_dump($numbers)" result is like this: string(5) "a","b" ($_GET['numbers'] get it's value like this: &numbers=a,b)

I know, maybe not the best solution, but actually I wanna transfer (in the end could be $_POST) some data to a "IN" statement including a prepared variable.

FOR REFERENCE: this is working:

$dbPrepare = $db->prepare(
    'SELECT
    `name`, `image`
    FROM `sometable`
    WHERE `number` IN ("'.implode('","', explode(',', $_GET['numbers'])).'")'
);

So I'm quite sure it's because of the prepared variable.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dqd72925 2014-06-26 08:02
    关注
    Try this
    
    $dbPrepare = $db->prepare(
        'SELECT
        `name`, `image`
        FROM `sometable`
        WHERE `number` IN (?)'
    );
    $dbPrepare->bind_param('s', $numbers[0],$numbers[1]);
    
    评论

报告相同问题?

悬赏问题

  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题