php - Select by two values (AND), and if nothing will be found select by each (OR) -


i have mysql query:

$sql = "select * cities "; if ($city) { $where .= ($where ? " , " : "")." ( city = '$city' ) "; } if ($street) { $where .= ($where ? " , " : "")."( street = '$street'"); }  if ($where) {    $sql .= $where;                $query = mysql_query($sql) or die(mysql_error($dbconnect_finder));  } 

i build query, if nothing found, use or operator instead of and. know can query again, if there no resoults. reduce costs, in 1 query.

edit: when user search $city , $street, , use $city = "londan" (insteed of london), $street = "somestreet". , operator not work. or operator should work. possible in 1 mysql query?

you can try like

$sql = "select * cities city = '$city' or street = '$street'"; 

or can try

$sql = "select * cities city = '$city'"; $result = mysql_query($sql); if(count($result) == 0) {    $sql .= " or street = '$street'";     $result = mysql_query($sql); } 

and since mysql_* functions deprecated better use mysqli_* functions or pdo statements.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -