0

I am trying to execute the following PHP code but am getting 0 rows in return even though there is a matching row when executed in command line:

$sql = "SELECT TOP 1 * FROM [Coupons] WHERE [code] LIKE '%?%' AND [platform] LIKE '%?%'";
$stmt = odbc_prepare($link, $sql);
odbc_execute($stmt, array($coupon,$platform));

How do I properly format the LIKE's in the query?

I tried

$sql = "SELECT TOP 1 * FROM [Coupons] WHERE [code] LIKE ? AND [platform] LIKE ?"

but that doesn't work either.

Any advice?

MonOve
  • 1,091
  • 13
  • 34

1 Answers1

1

Check this post out:

C# constructing parameter query SQL - LIKE %

You need to pass in the entire string, '%value%' as the parameter so in your case

$sql = "SELECT TOP 1 * FROM [Coupons] WHERE [code] LIKE ? AND [platform] LIKE ?";
$stmt = odbc_prepare($link, $sql);
odbc_execute($stmt,array("%{$coupon}%","%{$platform}%"));
Community
  • 1
  • 1
mmilleruva
  • 2,110
  • 18
  • 20
  • Thanks! $sql = "SELECT TOP 1 * FROM [Coupons] WHERE [code] LIKE ? AND [platform] LIKE ?"; $stmt = odbc_prepare($link, $sql); odbc_execute($stmt, array("%".$coupon."%","%".$platform."%")); – MonOve May 09 '14 at 14:28
  • Sorry I have never coded in PHP, but from what I read you can use that syntax: http://stackoverflow.com/questions/5605965/php-concatenate-or-directly-insert-variables-in-string . If it doesn't work, you can concatenate the strings however you want. Did the solution work for you? – mmilleruva May 09 '14 at 16:37