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
https://forums.phpfreaks.com/topic/76203-split-variable-then-query/
Share on other sites

some food for thought

 

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

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

"SELECT * FROM mydata WHERE ".implode(" or ", $arrMatches); 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.