dongnianchou7047 2015-08-24 20:03 采纳率: 0%
浏览 21

查询破坏代码中的内爆

I am working on building private message capability and the tutorial I am watching is based in mysql, so I am converting over to mysqli and avoiding creating an extra class for the following function the demonstrator created inside of it, so I am housing it in the same page.

I am receiving the follow error on my query

Warning: implode(): Invalid arguments passed in

My query is this...

$stmt = mysqli_query($con,"SELECT `id`, `username` FROM users WHERE username IN ('" . implode("', '", $user_names) . "')")

What am I doing wrong in my query to make this error come up? It is spot on what the demonstrator had. Could putting my function on the same page be the cause of this? Basically what he is trying to do is allow different users to be added in the 'To:' line so that it can be comma separated like.

Bob, Tim, George, etc

Full Code

function fetch_user_ids($user_names){
    foreach ($user_names as &$name){
        $name = mysqli_real_escape_string($name);
    }
}
$con = mysqli_connect("localhost", "", "", "");
$stmt = mysqli_query($con,"SELECT `id`, `username` FROM users WHERE username IN ('" . implode("', '", $user_names) . "')")
//$stmt = $con->prepare("SELECT `id`, `username` FROM users WHERE username IN ('" . implode("', '", $user_names) . "')")
    or die ("Messages query failed: %s
".($stmt->error));
//$stmt->execute();
//$stmt->bind_result($message_id, $message_username);

$names = array();

while (($row = mysqli_fetch_assoc($stmt)) !== false) {
    $names[$row['username']] = $row['id'];
}
return $names;
  • 写回答

1条回答 默认 最新

  • dongnao2582 2015-08-24 20:51
    关注

    The error you get means that you do not pass a valid array.

    Be sure that $user_names is an array with one dimension, such as:

    $user_names = ['Rob','Joe','Dan'];
    

    Secondly, you've a strange/difficult function fetch_user_ids(). Replace that function with a simple native php function:

    $user_names = array_map('mysqli_real_escape_string', $user_names);
    

    If the input $user_names is an array, the output is a sanatized array, so you can use the implode exactly as in your example query.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么