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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度