$sql_t=Array ( [Processor] => Array ( [0] => i3 ) [Memory] => Array ( [0] => 8GB [1] => 32GB ) )
$sql_s="SELECT * FROM table "
if(empty($sql_t))
$sql=$sql_s;
else{
$sql=$sql_s.'where ';
$first=true;
foreach($sql_t as $a =>$b){
foreach ($b as $x => $y){
if(count($b)==1){
$sql .=(($first) ? "":" AND ")."$a='$y'";
$first=false;}
else{
$sql .=(($first) ? "":" OR ")."$a='$y'";
$first=false;}
}
}
}
echo "<br/>query terms: ".$sql;
结果无法显示&#34;和&#34;和&#34;或&#34;。
例如:查询术语:SELECT * FROM table其中Processor =&#39; i3&#39;或记忆=&#39; 8GB&#39;或记忆=&#39; 32GB&#39;。
正确的ansower应该是&#34;查询术语:SELECT * FROM table其中Processor =&#39; i3&#39; AND Memory =&#39; 8GB&#39;或记忆=&#39; 32GB&#39;
答案 0 :(得分:0)
我无法修复你的代码。这是我的方法
$sql_s="SELECT * FROM table where ";
$sql_t= Array (
'Processor' => Array ( 'i3' ),
'Memory' => Array ('8GB','32GB' )
);
foreach($sql_t as $a =>&$b) {
if( count($b) > 1) {
$b = "`$a` in (\"". implode('", "', $b) .'")';
}
else {
$b = "`$a` = \"" . $b[0] . '"';
}
}
$sql = $sql_s . implode (' AND ', $sql_t);
// SELECT * FROM table where `Processor` = "i3" AND `Memory` in ("8GB", "32GB")