2013-04-23 61 views
1

我正在研究为什么看似正确的mysql查询没有返回正确的结果。我收到了一些文件,我相信这个(代码如下)是罪魁祸首所在。不幸的是,我没有拥有所有的文件(为朋友工作),所以我很难做任何var_dumps或测试。相反,我创建了自己的模拟搜索脚本,它返回正确的结果 - 尽管查询是相同的。使用MySQL查询返回正确的数据时遇到问题

原:

function build_generic_where_clause ($the_query_string) { 
    $where_clauses = Array(); 
    $the_query_string = $this->db->quote($the_query_string); 
    array_push($where_clauses, "accounts.name like '%$the_query_string%'"); 
    if (is_numeric($the_query_string)) { 
     array_push($where_clauses, "accounts.phone_alternate like '%$the_query_string%'"); 
     array_push($where_clauses, "accounts.phone_fax like '%$the_query_string%'"); 
     array_push($where_clauses, "accounts.phone_office like '%$the_query_string%'"); 
    } 

    $the_where = ""; 
    foreach($where_clauses as $clause) 
    { 
     if(!empty($the_where)) $the_where .= " or "; 
     $the_where .= $clause; 
    } 

    return $the_where; 
} 

但因为我得说两个项目像这样的失败:

Newstate Inc. 
Welders of New York 

和上面的查询只返回Newstate。但是,如果我在搜索框中键入%new,则返回两个项目。我需要得到它,只要输入'新'将返回他们两个。

我用于测试的模拟查询:

public static function getAccount($the_query_string){ 
    $con = drake::connectDB(); 

    $query = "select * from accounts where name like '%$the_query_string%'"; 

    $result = $con->query($query); 
    while($row = $result->fetch_array()){ 
     echo($row['name']).'<br />';  
    } 
} 

而对于“新”搜索时,一个成功返回两个项目。我最好的猜测是$ this-> db-> quote($ the_query_string);正在向搜索字符串添加一些东西,以某种方式改变它,所以它不会完全返回查询所做的事情。任何帮助将非常感激。

+0

运行phpmyadmin中形成的查询$ query – swapnesh 2013-04-23 17:30:54

+0

是的,在phpmyadmin中运行它也会成功返回这两个项目。 – tim 2013-04-23 17:36:00

+0

什么是在the_query_string? – swapnesh 2013-04-23 17:46:49

回答

0

我认为你可以使用

$the_query_string = mysql_real_escape_string($the_query_string) 

到eleminate任何意外或特殊的系统字符是escaped.Hope这将帮助你。 请让我知道评论中的状态。

+0

谢谢,我会让他给出一个答案。可能会有点拖延,但如果结果正确,我一定会将您的答案标记为正确。 – tim 2013-04-23 17:41:24

+0

怎么了?你的问题解决了? – 2013-04-24 08:41:57