我正在研究爲什麼看似正確的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);正在向搜索字符串添加一些東西,以某種方式改變它,所以它不會完全返回查詢所做的事情。任何幫助將非常感激。
運行phpmyadmin中形成的查詢$ query – swapnesh 2013-04-23 17:30:54
是的,在phpmyadmin中運行它也會成功返回這兩個項目。 – tim 2013-04-23 17:36:00
什麼是在the_query_string? – swapnesh 2013-04-23 17:46:49