1

Very strange behaviour. Using PHP 7.

When I run this query:

$sql = "SELECT * FROM product WHERE model = 'prefix_17867'";
$db_res = $db->query($sql);
$db_row = $db_res->fetch(PDO::FETCH_ASSOC);
print_r($db_row);

An array is printed with the fetched row.

Array ( [product_id] => 18108 [model] => prefix_17867 [quantity] => 10 [price] => 51.5574 )

However, if I concatenate a string (which will result in exactly the same string) no results seem to be fetched from database.

$prfx = "prefix_" . $code;  
$sql = "SELECT * FROM product WHERE model = '".$prfx."'";
$db_res = $db->query($sql);
$db_row = $db_res->fetch(PDO::FETCH_ASSOC);
print_r($db_row);

I echoed the query in both cases and comparing it they are exactly the same. Whats the problem? Maybe PDO doesn't let you concatenate in this way?

oderfla
  • 1,695
  • 4
  • 24
  • 49
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/202233/discussion-on-question-by-oderfla-php-pdo-only-gets-result-when-query-is-hardcod). – Samuel Liew Nov 12 '19 at 21:23

0 Answers0