Solved

PHP / MSSQL Query with LIKE clause

Posted on 2011-03-16
15
540 Views
Last Modified: 2013-12-13
Hello,

Im trying to run a MSSQL query within PHP and its working fine - however, if i replace a certain part of the query with a variable i get an error...

basically the code I have thats working: is as follows:

$PartNo1 = "123456";
$PartNo2 = "654321";

$query = "
select * from table
where Path LIKE '123456 -> 654321'
";

$runquery = mssql_query("$query");

echo 'Total records' . mssql_num_rows($runquery);


^ That works fine. now i'm trying to replace the LIKE clause with the variables instead of static text, so i want to replace:

where Path LIKE '123456 -> 654321'

WITH:

where Path LIKE '$PartNo1 -> $PartNo2'

but it fails... it always returns 0 results. - I've tried a few different combinations of variables and static text and it works some ways but not others.

where Path LIKE '123456 -> 654321'     --- works!

where Path LIKE '$PartNo1 -> 654321'   ----- works!

where Path LIKE '123456 -> $PartNo2' ---- doesnt work

where Path LIKE '$PartNo1 -> $PartNo2'   ----- doesnt work



I've echo'd the query as a whole, to see whats its generating and the query thats displayed is exactly what i want (123456 -> 654321) - if i take the code its generated and run it in sql management studio it works perfectly and gives me 4 rows of results.

Can anyone see where im going wrong? or how i can get around this?

Thanks










0
Comment
Question by:Bergstr
  • 4
  • 3
  • 3
  • +4
15 Comments
 

Author Comment

by:Bergstr
ID: 35148143
forgot to mention on the above code - all of the LIKE statements have a % at the end of the second part no.

(where Path LIKE '123456 -> 654321%' )
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35148236
Try this:

where Path LIKE '".$PartNo1." -> ".$PartNo2."'"
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35148249
If that does not work, I'm just curious about that > sign, but let's wait for the first result...
0
 
LVL 16

Expert Comment

by:Chris Harte
ID: 35148291
Same thing as Roads but I use single quotes

$query = "
select * from table
where Path LIKE '$PartNo1' -> '$PartNo2'
";
0
 

Author Comment

by:Bergstr
ID: 35148613
Just tried the following but none of them work:

(double quotes with overall single quotes)
where Path LIKE '".$PartNo1." -> ".$PartNo2."'
rows: 0

(single quotes)
where Path LIKE ''.$PartNo1.' -> '.$PartNo2.''
rows: (blank) - guessing that means the sql query failed

as i said though, if i echo the query, and then copy and paste the code into management studio it works fine..!
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35148884
please try:
where Path LIKE '{$PartNo1} -> {$PartNo2}'
0
 
LVL 2

Expert Comment

by:TimBare
ID: 35153240
Is the % in the $PartNo2 variable, or at the end of the query itself?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Expert Comment

by:TimBare
ID: 35153249
try this:
where Path LIKE '$PartNo1 -> ".$PartNo2."

Open in new window

(double quotes on either side of the .$PartNo2., and a single quote after the second double quote..
0
 
LVL 2

Expert Comment

by:TimBare
ID: 35153255
where Path LIKE '$PartNo1 -> ."$PartNo2."'

Open in new window


single quote didn't get copied.. my bad
0
 

Accepted Solution

by:
Bergstr earned 0 total points
ID: 35154726
Just tried all of the above with no success, and then i spotted what i thought could be a potential problem...

$PartNo2 was "654321 " not "654321" (extra space at the end)

This shouldnt really matter, because the Path cell contained 123456 -> 654321 -> 4444

- so there is actually a space after PartNo2 - with that in mind, i wouldnt expect it to be a problem? - guessing thats why it worked ok in sql management studio.

But to rule it out as a problem, i used str_replace to get rid of the spaces:

$part2 = str_replace (" ", "", $PartNo2);

and then using

where Path LIKE '$PartNo1 -> $part2%'

and it works perfectly :)

thanks for all the help



0
 
LVL 9

Expert Comment

by:crazedsanity
ID: 35156497
Just a thought: it is a LIKE search, and in the original question you posted what the queries looked like, only later to add the "%" character... is it possible that the "%" is getting left out by accident on the query that uses the variables?
0
 
LVL 6

Expert Comment

by:ramelong
ID: 35162588
I always works in that way (and I never had a problem):
$query = '
select * from table
where Path LIKE "'.$PartNo1.' -> '.$PartNo2.'"
';

Open in new window

0
 
LVL 6

Expert Comment

by:ramelong
ID: 35162590
I means "in this way"...
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35172653
You can as well use trim function
$part2 = trim($PartNo2);
0
 

Author Closing Comment

by:Bergstr
ID: 35364823
Found solution
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now