Perhaps this should be in the PHP forum but I'll try here first...
I'm trying to search some fields for complete words. The word can be in the middle of a string of text. My solution (perhaps there is a better one) is to use the following query:
[CODE=sql]
SELECT id, string FROM string_table WHERE string LIKE ('%This is my very long test string%') OR string LIKE ('% This %') OR string LIKE ('% very %') OR long LIKE ('% long %') OR test LIKE ('% test %') OR string LIKE ('% string %');
[/CODE]
This query actually works when I use MySQL Query Browser (or command line). You can see that each LIKE has a single word with spaces around. I don't want to match 'test' to attested' for example.
My problem arises when I send this query using PHP. I use something like this:
[CODE=php]
$output=array() ;
$query= "SELECT id, string FROM string_table WHERE string LIKE ('%This is my very long test string%') OR string LIKE ('% This %') OR string LIKE ('% very %') OR long LIKE ('% long %') OR test LIKE ('% test %') OR string LIKE ('% string %')";
$result= mysql_result($q uery,$link);
if ($result && mysql_num_rows( $result)>0) {
while ($row=mysql_fet ch_array($resul t)) {
$output=$row;
}
}
var_dump($outpu t);
[/CODE]
$output is an empty array. No results are returned so mysql_num_rows( )=0.
I think it's something to do with character encoding but I'm not really familiar with how to get PHP to send the query in the correct format so that the spaces are retained.
Can anyone help me?
Thanks!
I'm trying to search some fields for complete words. The word can be in the middle of a string of text. My solution (perhaps there is a better one) is to use the following query:
[CODE=sql]
SELECT id, string FROM string_table WHERE string LIKE ('%This is my very long test string%') OR string LIKE ('% This %') OR string LIKE ('% very %') OR long LIKE ('% long %') OR test LIKE ('% test %') OR string LIKE ('% string %');
[/CODE]
This query actually works when I use MySQL Query Browser (or command line). You can see that each LIKE has a single word with spaces around. I don't want to match 'test' to attested' for example.
My problem arises when I send this query using PHP. I use something like this:
[CODE=php]
$output=array() ;
$query= "SELECT id, string FROM string_table WHERE string LIKE ('%This is my very long test string%') OR string LIKE ('% This %') OR string LIKE ('% very %') OR long LIKE ('% long %') OR test LIKE ('% test %') OR string LIKE ('% string %')";
$result= mysql_result($q uery,$link);
if ($result && mysql_num_rows( $result)>0) {
while ($row=mysql_fet ch_array($resul t)) {
$output=$row;
}
}
var_dump($outpu t);
[/CODE]
$output is an empty array. No results are returned so mysql_num_rows( )=0.
I think it's something to do with character encoding but I'm not really familiar with how to get PHP to send the query in the correct format so that the spaces are retained.
Can anyone help me?
Thanks!
Comment