473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

103 100+
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!
Jul 21 '07 #1
4 12469
mwasif
802 Expert 512MB
What does mysql_num_rows() return? Use var_dump($row) inside while() to see the output.
Jul 21 '07 #2
pbmods
5,821 Expert 4TB
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.'?).
Jul 22 '07 #3
henryrhenryr
103 100+
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?
Jul 25 '07 #4
mwasif
802 Expert 512MB
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.
Jul 25 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: poff | last post by:
I have written a forum and am using the following query to search in it: $query="select topics.tid,f_messages.messid from f_messages left join topics on f_messages.tid = topics.tid where...
6
by: Xenophobe | last post by:
I know this isn't a MySQL forum, but my question is related to a PHP project. I have two tables. table1 table2 "table1" contains 2 columns, ID and FirstName:
2
by: aj70000 | last post by:
This is my query select ano,max(date),a_subject from MY_TAB where table_name='xyz' and ano=877 group by a_subject,ano order by a_subject ANO max(Date) A_Subject 877 2005-01-20...
14
by: signaturefactory | last post by:
I am trying the following query in and oleDbCommand: SELECT PartLocations.LocationName, Sum(PartsJournal.Quantity) AS SumOfQuantity, PartsJournal.PartsLotNumber FROM PartLocations INNER JOIN...
1
by: Ranga | last post by:
Hi, I am getting the following error when i am using query no in my sql statement and compiling the stored proc. I am doing this on Unic UDB DB2. Please let me know if i am mising something here....
5
by: Bob Alston | last post by:
I have a where used utility that shows where any table or query is used - in a query, form or table. I would like some kind of diagram that shows these relations. I have some reports that...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
8
by: p | last post by:
I'm trying to put a query into VBA code and its giving me difficulties. I would like to put the following query: SELECT tbl_Workload.Priority, tbl_Workload.Number AS Num, tbl_Workload.Name,...
0
Walt in Decatur
by: Walt in Decatur | last post by:
I have a form which is tied to a table with data for equipment belonging in a particular space (tbl_equpment_data). This form is actually a subform on a main form which also deals with other data...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.