2012-08-24 115 views
0

我想知道如果有人能幫助我。下面的代碼將生成type = bus的所有用戶,但不會生成只有批准= 1的用戶。如果有人能指出我的錯誤,我會很感激。搜索查詢被某人通過表單輸入到文本框中的字詞激活,這部分工作正常,只有那些搜索詞在那裏配置文件出現,只有他們與type = bus但批准= 1似乎沒有影響和返回所有批准,我真的希望有人可以幫助。multiple where子句php

$query = "select * from users where type='bus' and approved=1 and 
name like \"%$trimmed%\" 
or profile_words like \"%$trimmed%\" 
or full_name like \"%$trimmed%\" 
or tag_line like \"%$trimmed%\" 
or referral like \"%$trimmed%\" 
or profession like \"%$trimmed%\" 
order by full_name"; 

回答

0
$query = "select * from users where type='bus' and approved=1 and 
(name like \"%$trimmed%\" 
or profile_words like \"%$trimmed%\" 
or full_name like \"%$trimmed%\" 
or tag_line like \"%$trimmed%\" 
or referral like \"%$trimmed%\" 
or profession like \"%$trimmed%\") 
order by full_name"; 

1

組你的條件就是這樣,

$query = " 
select * 
from users 
where type='bus' and 
     approved = 1 and 
      (
      name like \"%$trimmed%\" or 
      profile_words like \"%$trimmed%\" or 
      full_name like \"%$trimmed%\" or 
      tag_line like \"%$trimmed%\" or 
      referral like \"%$trimmed%\" or 
      profession like \"%$trimmed%\ 
     ) 
order by full_name"; 

我有一個建議給你時,請使用PDOMySQLI擴展名從SQL注入,以避免。

例PDO的,

<?php 

$query = " 
select * 
from users 
where type='bus' and 
     approved = 1 and 
      (
      name like ? or 
      profile_words like ? or 
      full_name like ? or 
      tag_line like ? or 
      referral like ? or 
      profession like ? 
     ) 
order by full_name"; 

$name = '%' . $trimmed . '%'; 

$stmt = $dbh->prepare($query); 
$stmt->bindParam(1, $name); 
$stmt->bindParam(2, $name); 
$stmt->bindParam(3, $name); 
$stmt->bindParam(4, $name); 
$stmt->bindParam(5, $name); 
$stmt->bindParam(6, $name); 

$stmt->execute(); 

?> 
0

我覺得你的一部分批准的是好的,但我認爲問題可能出在OR的起點。嘗試通過分離ANDOR語句像這樣把括號:

$query = "select * from users where type='bus' and approved=1 and 
(name like \"%$trimmed%\" 
or profile_words like \"%$trimmed%\" 
or full_name like \"%$trimmed%\" 
or tag_line like \"%$trimmed%\" 
or referral like \"%$trimmed%\" 
or profession like \"%$trimmed%\") 
order by full_name"; 
0

支架的地方運營商。目前您正在獲取

where (type='bus' and approved=1) 
or .... [all the other operators]