Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Kann eine PHP-MYSQL-Abfrage leere Variablen in der WHERE-Klausel ignorieren?

Verwenden Sie

$where = "WHERE user_id = '$username'";

if(!empty($value1)){
$where .= "and location = '$value1'";
}

if(!empty($value2 )){
$where .= "and english_name= '$value2 '";
}


$query = "SELECT * FROM moth_sightings $where";
$result = mysql_query($query) or die(mysql_error());
$r = mysql_numrows($result);