请教sql语句写法?
$sql1是根据用户输入选择值动态生成的:
$ConditionsNumber=6;
$ConditionsArray=array( "$condition1 ", "$condition2 ", "$condition3 ", "$condition4 ", "$condition5 ", "$condition6 ");
$SearchSQLArray=array( " where ip= '$condition1 ' ", " where bianhao= '$condition2 ' ", " where name= '$condition3 ' ", " where user= '$condition4 ' ", " where bankid= '$condition5 ' ", " where equimode= '$condition6 ' ");
for($i=0;$i <$ConditionsNumber;$i++)
{
if($ConditionsArray[$i]== " ")
$SearchSQLArray[$i]= " ";
$haveWhere=false;
for($j=0;$j <$i;$j++)
{
$wherePosition=strpos($SearchSQLArray[$j], "where ");
if(($wherePosition== "1 ")&&($haveWhere==false))
{
$SearchSQLArray[$i]=ereg_replace( "where ", "and ",$SearchSQLArray[$i]);
$haveWhere=true;
}
}
};
for($i=0;$i <$ConditionsNumber;$i++)
$sql1=$sql1.$SearchSQLArray[$i];
$sql = "select * from oareport ".$sql1. "; ";
condition6是设备类型,选所有类型时equimode值为 ' '。
语句正常运行,现在我想排除设备类型为 'PC '的,即equimode <> 'pc ':
$sql = "select * from oareport ".$sql1. "and equimode <> 'pc ' ; ";
可是执行时如设备类型选所有类型时生成的sql语句错误,请问应该怎样做?
[解决办法]
晕,看不懂
[解决办法]
and前没有空格?
[解决办法]
不知道设备类型选所有类型是现在的那个样子?
$sql = "select * from oareport where equimode <> ' ' and equimode <> 'pc ' ; ";
OR
$sql = "select * from oareport and equimode <> 'pc ' ; ";
如果都为空的话,你的代码应该是没有限制的。
判断下这个$ConditionsArray数组,都为空的话,那么就是where equimode <> 'pc ',而不是and equimode <> 'pc '。
[解决办法]
$sql = "select * from oareport ".$sql1. "and equimode <> 'pc ' ; ";
=====================================
这句sql似乎有语法错误,echo 出来看看。
[解决办法]
好复杂的语句~