2012-12-29 82 views
0
function comp_post_code($comp_post_code){ 
global $host, $dbname, $user, $pass; 
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); 
$DBH->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); ///*** for error handling 
$STH = $DBH->prepare("SELECT * from uk_data where 
comp_post_code like :comp_post_code and cat1 like :cat"); 
$STH->bindValue(':cat', "%$cat1%", PDO::PARAM_STR); 
$STH->bindValue(':comp_post_code', "%$comp_post_code%", PDO::PARAM_STR); 
$STH->execute(); 
$STH->setFetchMode(PDO::FETCH_ASSOC); 
return $STH; 
} 

選擇多條記錄,我有CAT1到cat10 ..我想獲得的數據輸出,而無需編寫像短查詢

and comp_post_code like :comp_post_code and cat2 like :cat 
and comp_post_code like :comp_post_code and cat3 like :cat 

我有出路,這個長碼......我們可以翁寫代碼並退出,但仍然想知道我們可以用其他方式做什麼?

表結構

comp_post_code CAT1 CAT2 CAT3 CAT4
+0

你可以張貼表結構? (cat1 cat10是表格欄?) – FlorinelChis

+0

@FlorinelChis是你是,我有編輯問題... – Harinder

+0

:貓應該在* all * cat [1-n]或任何*列中找到? – FlorinelChis

回答

1

根據您的意見細節:

SELECT * from uk_data where 
comp_post_code like :comp_post_code AND (
cat1 like :cat OR 
cat2 like :cat OR 
cat3 like :cat OR 
cat4 like :cat OR 
cat5 like :cat OR 
cat6 like :cat OR 
cat7 like :cat OR 
cat8 like :cat OR 
cat9 like :cat OR 
cat10 like :cat 
) 
+0

Thx .........;) – Harinder