Frage

SQL IN OPERATOR


I use the IN operator because I work with arrays in my SQL statement.

For example:

$city[1] = "'Paris'";
$city[2] = "'London'";
$cityString= implode(", ", $city);

SELECT * FROM Customers
WHERE City IN ($cityString);

But in my code I use the value's of my GET or POST array. But when the array is empty there are no values, so my SQL statement won't work. How can I solve this without if statements and have a list of thousands of SQL statements.

War es hilfreich?

Lösung

You can check if the array is empty:

if(!empty($city)) {
    $query = "SELECT * FROM Customers WHERE City IN ($cityString)";
    $result = mysql_query($query);

} else {
    // .. do something else ?
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top