1

In my MSQL table, one row has the column code as "1001000181026".

My PHP script is only interested in the bold part (1810), and I use the underscore wildcards (_) for the other characters of code.

But: It doesn't work with PDO (0 results)! On the other hand, if I write the SELECT code manually into phpMyAdmin, it works well (1 result).

Here is the PDO/PHP-code (which is not working):

$code = '_______1810__'; 
$preparequery = 'SELECT * FROM table WHERE code LIKE :code';
$stmt = $conn->prepare($preparequery);
$stmt->bindValue("code", $code);
$y = $stmt->rowCount();

echo '<p>We have '.$y.' row(s).</p>'; // We have 0 row(s).

The query in phpMyAdmin works fine:

SELECT * FROM `table` WHERE code LIKE '_______1810__'

This gives me 1 result, as expected.

What could be wrong with the PHP code (PDO)? Thank you for your help!

anmipa
  • 93
  • 1
  • 8
  • I am not sure if it may have something to do with what is mentioned there: https://stackoverflow.com/questions/5809951/pdo-valid-characters-for-placeholders according to which PDO placeholders (e.g. ":code") allow "alphanumeric + **underscore**" characters. – anmipa Oct 28 '18 at 20:17

1 Answers1

0

You could change your query to:

SELECT * FROM table WHERE code LIKE CONCAT('_______', :code, '__')

This way you don't need to worry how the placeholders work. Also the PHP side will be a little more simplified.

mdh.heydari
  • 540
  • 5
  • 13