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
我有办法解决这个问题......我们可以编写ong代码并退出,但仍然想知道我们可以用其他方式吗?
表格结构
答案 0 :(得分: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
)