Solved

PHP / MSSQL Query with LIKE clause

Posted on 2011-03-16
15
545 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Checking if varaible is empty 6 38
PHP populating an array. 4 22
php simple error message 4 22
Creating Functions in phpMyAdmin 8 9
This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
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…

813 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

10 Experts available now in Live!

Get 1:1 Help Now