Jump to content

Split variable then query


Canman2005

Recommended Posts

Hi all

 

I have a search form which has a field with the id of "keyword"

 

I then have a query

 

SELECT * FROM mydata WHERE `content` LIKE '%".$_GET['keyword']."%'

 

This then returns results.

 

Is it possible to split the value of the form field, then query each word separate?

 

For example, if I enter "internet design" into the field and then run a query, it would query the words "internet" and "design" separate. So it basically splits the value and queries both words.

 

Is this possible?

 

Thanks in advance

 

Dave

Link to comment
Share on other sites

Thanks rajivgonsalves

 

I've been playing around with what you suggest and ended up with the following

 

$arrSearches  = explode(" ",$_GET['keyword']);
$arrMatches = array();

foreach ($arrSearches as $strKey => $strValue)
{
array_push($arrMatches," `content` like '%$strValue%' ");
}

$sql = "SELECT * FROM mydata WHERE ".implode(" or ", $arrMatches)"";
$query = @mysql_query($sql,$connection) or die(mysql_error());
while ($row = mysql_fetch_array($query))
{
print $row['content'];
}

 

But im getting the error

 

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/npa/public_html/members/search/searchscript.php on line 10

 

Do you know where im going wrong?

 

Sorry, im only good at simple queries (but learning)

 

Thanks in advance

 

Dave

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.