PHP if: Elseif...else problems
This post was last edited by xytianshiwx on 2013-12-08 10:09:44
$field _func= "count| Avg| First| Last| Max| min| sum| TOP ";
$where _field= "Where|or|xor|and|in";
$param = "name";
$sql _param_list=array ();
if (False!==strpos ($param, ",")) {//Determine existence ","
$sql _param_list["field"] = $param;
}else{
if (Preg_match ("/\b (". Strtolower ($field _func). ") \b\ ((\*|[ A-za-z_ ']) (+\)/i ", $param)) {
$sql _param_list["field"] = $param;
}elseif (Preg_match ("/\b (". $where _field. ") \b/i ", $param) | | Preg_match ("/(>|>|=)/i", $param)) {//Determine where
$sql _param_list["where"] = $param;
}elseif (Preg_match ("/\b (order|limit|group|) \b/i ", $param, $param _name)) {//Judgment Order|limit|group
Switch ($param _name[0]) {
Case "Order": $sql _param_list["order"] = $param;
Break
Case "Limit": $sql _param_list["limit"] = $param;
Break
Case "group": $sql _param_list["group"] = $param;
Break
}
}else{
$sql _param_list["field"] = $param;
}
}
Print_r ($sql _param_list);
$param not assigned a $sql_param_list["field"??
Logic error or other error, please expert pointing pointing!!!
Share to: more
------Solution--------------------
Array
(
[WHERE] = name>10
)
Why is $param assigned to $sql _param_list["field"?
Your $param is "name>10" and there's No ","
only if (Preg_match ("/\b (". $where _field. ") \b/i ", $param)
------Solution--------------------
Preg_match ("/(>
------Solution--------------------
>
------Solution--------------------
=)/I ", $param)) {//
Branch can enter
------Solution--------------------
}elseif (Preg_match ("/\b (Order
------Solution--------------------
Limit
------Solution--------------------
Group
------Solution--------------------
) \b/i ", $param, $param _name)) {//Judgment order
------Solution--------------------
Limit
------Solution--------------------
Group
==> "/\b (Order
------Solution--------------------
Limit
------Solution--------------------
Group) \b/i ", //regular more than one or
------Solution--------------------
if (False!==strpos ($param, ",")) {//Determine existence ","
That would be even more wrong, a single name would not have "," the
According to the grammar paradigm of SQL instruction
You should read the substring separated by a space character, and determine which grammatical component it belongs to.
And each section of the SQL instruction is a specific identifier.