Forum Moderators: coopster

Message Too Old, No Replies

MySQL wildcard search issue

multiple variable and doesnt work

         

xboxingman

3:41 am on Jan 29, 2012 (gmt 0)

10+ Year Member



I have been trying to get this to work for a few days now. I cannot figure out how to search the database and get the results I want.

I have inputs in the db like 'red-blue-green-orange' 'blue-green-red' 'red' etc. it could be any combination depending on what order the user input them.

Now on the search they can choose the same options from a multiple select drop down.

This $_POST['var'] is an array. (red, blue, green, etc)

say they searched for 'red' I am trying to use a wildcard but it isn't working how I expected, I am not sure if the hyphen between them in the database is the problem. If so maybe I should insert them with a space & hyphen " - " (some vars are actually multiple words, hence the hyphen is used in the explode() )

I was trying something like:

$var = $_POST['var'];
$query = "SELECT * FROM table WHERE color LIKE '%". $var ."%'";



however since this is an array in the post I thought that was the issue so I did:

if(is_array($_POST['var'])) {
$var_array = $_POST['var'];
$count = count($var_array);
$query = "SELECT * FROM table WHERE color LIKE '%". $var_array[0] ."%'";
$x = 1;
while($x <= $count) {
$query .= " OR LIKE '%". $var_array[$x] ."%'";
$x++;
}
}



Am I going about this wrong or is there a better way that might work?
Thanks a lot!
Mike

xboxingman

3:51 am on Jan 29, 2012 (gmt 0)

10+ Year Member



I seen a type of mine, on $query .= " OR LIKE... "
my code does say:

$query .= " OR color LIKE '%".var_array[$x]."%'";

xboxingman

5:07 am on Jan 29, 2012 (gmt 0)

10+ Year Member



This is my actual full query code...
I had all the vars with ". $var ." but it changed it a few minutes ago just to try something else



$search_query="SELECT * FROM table WHERE
gender = '$gender' AND";
$count = count($bodytype);
$search_query .= " body_type LIKE '%$bodytype[0]%'";
$x = 1;
if(($count > 1) && ($bodytype[1] != "") ) {
while($x <= $count) {
$search_query .= " OR body_type LIKE '%$bodytype[$x]%'";
$x++;
}
}
$search_query .= " AND height = '$height' AND
hair_len = '$hairlength' AND";
$count2 = count($haircolor);
$search_query .= " hair_col LIKE '%$haircolor[0]%'";
$x2 = 1;
if(($count2 > 1) && ($haircolor[1] != "") ) {
while($x2 <= $count2) {
$search_query .= " OR hair_col LIKE '%$haircolor[$x2]%'";
$x2++;
}
}
$search_query .= " AND eye_col = '$eyecolor' AND
cloth_size = '$size' AND
sex_pref = '$preference' AND
weight = '$weight' AND";
$count3 = count($nation);
$search_query .= " nation LIKE '%$nation[0]%'";
$x3 = 1;
if(($count3 > 1) && ($nation[1] != "") ) {
while($x3 <= $count3) {
$search_query .= " OR nation LIKE '%$nation[$x3]%'";
$x3++;
}
}
$mysql_query .= " AND";
$count5 = count($fetish);
$search_query .= " fetish LIKE '%$fetish[0]%'";
$x5 = 1;
if(($count5 > 1) && ($fetish[1] != "")) {
while($x5 <= $count5) {
$search_query .= " OR fetish LIKE '%$fetish[$x5]%'";
$x5++;
}
}
$search_query .= " AND race = '$ethnicity' AND";
$count4 = count($bodydec);
$search_query .= " body_dec LIKE '%$bodydec[0]%'";
if(($count > 1) && ($bodydec[1] != "") ) {
$x4 = 1;
while($x4 <= $count4) {
$search_query .= " OR body_dec LIKE '%$bodydec[$x4]%'";
$x4++;
}
}
$mysql_query .= " AND skin_tone = '$skintone'";

xboxingman

9:10 pm on Jan 29, 2012 (gmt 0)

10+ Year Member



I do see I had an error where it says $mysql_query instead of being $search_query but that is fixed. It still doesn't bring any results when searching for the exact criteria I know to be in the db..

henry0

9:26 pm on Jan 29, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a quick one as I need to go
try a preg_replace()to replace hyphen with space
now you should have a workable array