Connecting Tech Pros Worldwide Forums | Help | Site Map

How to query with a space ' ' in the LIKE clause

Member
 
Join Date: Jun 2007
Posts: 101
#1: Jul 21 '07
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:

Expand|Select|Wrap|Line Numbers
  1. 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 %');
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. $output=array();
  2. $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 %')";
  3. $result= mysql_result($query,$link);
  4. if ($result && mysql_num_rows($result)>0) {
  5.   while ($row=mysql_fetch_array($result)) {
  6.     $output=$row;
  7.   }
  8. }
  9. var_dump($output);
  10.  
$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!

mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 719
#2: Jul 21 '07

re: How to query with a space ' ' in the LIKE clause


What does mysql_num_rows() return? Use var_dump($row) inside while() to see the output.
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#3: Jul 22 '07

re: How to query with a space ' ' in the LIKE clause


Heya, Henry.

In your PHP, you use $output ->=<- $row instead of $output ->.=<- $row. Is this intentional?

You may want to use MySQL's fulltext searching instead (what about ' test.' or '-test ' or '-test.'?).
Member
 
Join Date: Jun 2007
Posts: 101
#4: Jul 25 '07

re: How to query with a space ' ' in the LIKE clause


Hi!

Thanks for your replies - sorry it's taken so long to respond! Trying to juggle too many things!

The $output=$row is a typo. Should read $output[$row['id']]=$row; - I record each row as a 'sub-array' with the primary key as the array key.

I tried outputing the results as the while loop looped. Hasn't revealed much. The results are output row by row but they are just the results that were displayed by my script anyway.

The problem is rooted in the fact that searching on something like MySQL query browser gives me the correct result, while using my script and the same query gives me different results?
mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 719
#5: Jul 25 '07

re: How to query with a space ' ' in the LIKE clause


Quote:

Originally Posted by henryrhenryr

The problem is rooted in the fact that searching on something like MySQL query browser gives me the correct result, while using my script and the same query gives me different results?

This is not possible. Make sure you are connecting to correct database and table. Echo the the resultant query in PHP and execute it in MySQL to compare the results.
Reply