php - Is there a wildcard operator I can use in a WHERE clause? -


i have mysql query benefit wildcard-style operator. unfortunately, internet searches proving difficult can't figure out how narrow down search keys.

i have, example...

$my_color = "red"; $a = $pdo->prepare("select * my_table color=:color"); $a->bindparam('color', $my_color); $a->execute(); $data = fetchall(pdo::fetch_assoc); 

this works great. i'd give customers ability search color. yes, (basically) if-then around entire pdo block create multiple queries, i'm hoping there's more elegant solution.

does mysql have ability set condition translates to, "accept anything"? assuming fictional mysql command, any, ability set variable...

$my_color = "any"; 

and rows of color.

use like instead of =. if value doesn't contain wildcard characters, like performs exact equivalence check. can use % wildcard match anything. so:

$color = '%'; $a = $pdo->prepare("select * my_table color :color"); $a->bindparam('color', $my_color); $a->execute(); $data = fetchall(pdo::fetch_assoc); 

however, better option might put logic in php rather sql.

if ($color) {     $a = $pdo->prepare("select * my_table color=:color");     $a->bindparam('color', $my_color); } else {     $a = $pdo->prepare("select * my_table"); } $a->execute(); 

see https://stackoverflow.com/a/28909923/1491895 more general technique building where clause dynamically multiple form inputs.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -