PHP / MSSQL Query with LIKE clause

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










BergstrAsked:
Who is Participating?
 
BergstrConnect With a Mentor Author Commented:
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
 
BergstrAuthor Commented:
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
 
Lukasz ChmielewskiCommented:
Try this:

where Path LIKE '".$PartNo1." -> ".$PartNo2."'"
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Lukasz ChmielewskiCommented:
If that does not work, I'm just curious about that > sign, but let's wait for the first result...
0
 
Chris HarteThaumaturgeCommented:
Same thing as Roads but I use single quotes

$query = "
select * from table
where Path LIKE '$PartNo1' -> '$PartNo2'
";
0
 
BergstrAuthor Commented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
please try:
where Path LIKE '{$PartNo1} -> {$PartNo2}'
0
 
TimBareCommented:
Is the % in the $PartNo2 variable, or at the end of the query itself?
0
 
TimBareCommented:
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
 
TimBareCommented:
where Path LIKE '$PartNo1 -> ."$PartNo2."'

Open in new window


single quote didn't get copied.. my bad
0
 
crazedsanityCommented:
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
 
ramelongCommented:
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
 
ramelongCommented:
I means "in this way"...
0
 
Lukasz ChmielewskiCommented:
You can as well use trim function
$part2 = trim($PartNo2);
0
 
BergstrAuthor Commented:
Found solution
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.