PHP and MySQL Queries

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

Hello
I'm having a bit of trouble here...
Here's whats happening in PHP:
I have a list of words in a string variable.
I split them using preg_split()
I want the PHP to search a MySQL database for each word in the variable

IE:

<?php
$list
= \"web internet games\";
$array = preg_split(\"/ /\", $list);
$a = count($array);
$i = 0;
while (
$a > $i) {
$query = \"SELECT * FROM table WHERE (column LIKE '%$array[$i]%') ORDER BY column ASC \";
$result = mysql_query($query)
?>

Then it would get the results and parse them to a browser etc...
The trick is, I'm using If and Elseif conditionals depending on what is searched for... so if category=list then it would do what is above, but if category=list2 then it would search the database for a different set of keywords that I have in there... Confusing huh...
Well I've tried it this way, and it only searches the MySQL table for the first one in the list... IE the one above would only search for web not internet and games.
If possible, I'd like for it to alphebetize all the results... not just each search

Any idea??
I'm confused and I've been looking at different ways for the past 3 days nonstop! UGH!Confused :confused:

Laughing out loud

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I'm not entirely sure what you want..

is this what you want:

<?php
$list
= \"web internet games\";
$tmp_where = \"column LIKE '%\" . implode(\"%' OR column LIKE '%\", explode(\" \", $list)) . \"%'\";
$query = \"SELECT * FROM table WHERE ($tmp_where) ORDER BY column ASC \";
$result = mysql_query($query);
?>

Mark Hensler
If there is no answer on Google, then there is no question.

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

Laughing out loud :D Laughing out loud
Seems to work great
Thanks a lot!
I'll be back if it doesn't work...
Thanks!!!!!!!!!!!!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.