I have a section of code that takes a form, with numerous drop down boxes, and runs the results through a mysql database for rows that have the exact wording in them. So say in the drop down box you have three options, No Preference, Fun, Not Fun.

Currently my code only returns results that equal Fun or equal Not Fun. I want to change it so that the code returns rows that contain the word Fun or the words not fun. Here is mysql statement for it:

$strQuery = "select * database " . (trim($strAddQuery) != ""?" where 1=1 ":"") . $strAddQuery . " order by Name";

I know I have to change something in this section of code:

" . (trim($strAddQuery) != ""?" where 1=1 ":"") . $strAddQuery . "

I'm just sort of at a wall and having a hard time figuring it out. This is one of the more complicated things I've done in php (newbie obviously).

$strAddQuery is:

$strAddQuery = $arrQueryParts['a'] . $arrQueryParts['b'] . $arrQueryParts['c']. $arrQueryParts['d']. $arrQueryParts['e'];

Any help is greatly apreciated.

5 Years
Discussion Span
Last Post by simplypixie
$strQuery = "SELECT * FROM tablename WHERE column_name LIKE '%Fun%' ORDER BY name"

If your table is called database I would highly recommend changing it's name to prevent issues.


My database isn't named that. Just through that in for the forum. I do know how to write a basic Like statement but because it's arrays it throws me off. I know what I want to check against, the array, but I don't know what exactly I should check it against and if I need to use a more complicated statement. Currently the statement I have below returns all results from the database. Thanks but I don't quite have it yet.

$strQuery = "select * from database Where 'Trim($StrAddQuery)' Like '%$StrAddQuery%' order by Name";

Therefore you will need to split your array to be each value separated by OR LIKE, something like this:

$strAddQuery = "column_name LIKE '%{$arrQueryParts['a']}%'";
$strAddQuery .= " OR column_name LIKE '%{$arrQueryParts['b']}%'";
$strAddQuery .= " OR column_name LIKE '%{$arrQueryParts['c']}%'";
$strAddQuery .= " OR column_name LIKE '%{$arrQueryParts['d']}%'";
$strAddQuery .= " OR column_name LIKE '%{$arrQueryParts['e']}%'";
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.