PHP multi-condition Query Processing
There are many places to write such queries in SQL, so the query should be enriched.
// Method 1
// Obtain the dictionary type
$ Sztm = trim ($ _ request ['ztm ']);
$ Stmpy = trim ($ _ request ['tmpy']);
$ Sssh = trim ($ _ request ['ssh ']);
$ Scbm = trim ($ _ request ['cbm']);
$ SQL = 'select * From ts_gcb ';
If ($ sztm! = ''){
$ SQL. = 'where ZTM like "% '. $ sztm.' % "';
}
If ($ stmpy! = ''){
$ SQL. = (preg_match ('/where/', $ SQL )? 'And': 'where'). 'tmpy like "% '. $ stmpy.' % "';
}
If ($ Sssh! = ''){
$ SQL. = (preg_match ('/where/', $ SQL )? 'And': 'where'). 'ssh like "% '. $ Sssh.' % "';
}
If ($ scbm! = ''){
$ SQL. = (preg_match ('/where/', $ SQL )? 'And': 'where'). 'cbm like "% '. $ scbm.' % "';
}
Echo $ SQL;
$ Conn = mysql_connect ($ dbhost, $ dbuser, $ dbpwd );
Mysql_select_db ($ dbname );
$ Rst = mysql_query ($ SQL, $ conn );
/// Method 2
If (empty ($ ISBN ))
{
$ Where [] = "'isbn 'like' % $ ISBN % '";
}
Implode ('and', $ where );