Spelling tips for multiple query criteria for SQL statements.
Spelling tips for multiple query criteria for SQL statements.
$sql = "SELECT * from TB1";
if ($id =$_get[' id ')
{
$where. = "where id like"% $id% "";
}
if ($name =$_get[' name ')
{
$where. = "where name like"% $name% "";
}
When the ID has a value
Sql=select * from tb1 where id like "% $id%"
When name has a value
Sql=select * from TB1 where name like "% $name%"
When the value is at the same time, SQL is wrong.
Of course you can use it.
if ($id =$_get[' id ']&& $name =$_get[' name '])
{
Where.= "and";
}
I cite only two conditions, the actual project I have more than 10 conditions, this way certainly not.
Find a better way to stitch
Share to:
------Solution--------------------
$where = Array ();
foreach ($_get as $k =>v) $where [] = "$k like '% $v% '";
$sql = "SELECT * from TB1";
if ($where) $sql. = ' where '. Join (' and ', $where);
------Solution--------------------
$sql = "SELECT * from TB1";
$where = Array ();
if ($id =$_get[' id ')
{
$where []= "id like '% $id% '";
}
if ($name =$_get[' name ')
{
$where []= "name like '% $name% '";
}
$s = (!empty ($where))? "Where". Implode ("and", $where): ';
$sql. = $s;
------Solution--------------------
Personally, I think I'm a simple
$sql = "SELECT * from tb1 where 1=1";
if ($id =$_get[' id ') $sql. = "and id like"% $id% "";
if ($name =$_get[' name ') $sql. = "and name like"% $name% "";
------Solution--------------------
Citation:
I personally think I'm this simple
$sql = "SELECT * from tb1 where 1=1";