dongnang8192 2014-02-16 23:07
浏览 23
已采纳

内爆阵列和搜索匹配mysql php

I'm trying to take an array and implode it and than run it through a mysql query to search my database for matches. If there are matches, I wanna return the matching values. It keeps returning false and I'm not sure why. I did a vardump and can see the array is there, but doesn't seem to be getting passed to the mysql_query. If I manually put the array into the query it works no problem. Any ideas?

My Array (This comes from my Android App):

$refids = (jdu23764js84, 2746272jsjs7f, 39823874hbsjsk)

PHP script code:

public function searchList($refids) {
    $refarray = array($refids);        
    $comma_separated = implode(',', $refarray);            
    $result = mysql_query("SELECT `ref_id` FROM `main` WHERE `ref_id` IN
    ({$comma_separated})");        
    if ($result == true){  
        $result = mysql_fetch_array($result);                     
            return $result;                
        } else {
          return false; 
    }
  • 写回答

1条回答 默认 最新

  • dor65412 2014-02-16 23:17
    关注

    You've forgotten to quote the individual values inside your $refids, so you're building

    ... WHERE `ref_id` IN (jdu23764js84, 2746272jsjs7f, ...)
    

    and MySQL is interpreting those as field names. In other words, you're suffering from an SQL injection attack vulnerability, and your utter lack of ANY error handling on the database code is preventing from seeing the errors mysql is trying to tell you about:

    $csv = implode("','", $refarray);
                    ^-^-- note the addition of the quotes:
    
    $sql = "SELECT .... `ref_id` IN ('{$csv}')";
                                     ^------^--- again, note the quotes
    

    This fixes the problem in the short term. In the long term, you need to read through http://bobby-tables.com and learn what it has to tell you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决