Solved

PHP / MSSQL Query with LIKE clause

Posted on 2011-03-16
15
548 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 17

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 143

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
 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Extracting content from meta tag PHP MYHTML DOM 3 21
Ahax pagination 9 33
Ajax and PHP 4 31
insert ID of database value into database instead of text value from text field 23 30
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 …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

830 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