0

When I'am passing $id variable in bindValue('id', $id) it shows empty array but when I pass number in bindValue('id', 2) ,I'm getting the result in array.

$em = $this->getDoctrine()->getManager();
$connection = $em->getConnection();
$statement = $connection->prepare("SELECT s.subtitle FROM submenu as s where s.menu_id= :id");
$statement->bindValue('id', $id);
$statement->execute();
$results = $statement->fetchAll();
dump($results);

Please could anybody tell me whats my mistake in above code. I am getting empty result if I pass variable in bindvalue function but if I use this bindValue('id',2) I'm getting result.

Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
Narayan Ghimire
  • 165
  • 1
  • 3
  • 9

1 Answers1

0

If $id is an integer the it should work but if not then may be some whitespace is coming in your variable (for example $id = ' 4redtr54r') In this case you will get null value so can you please check your variable before assigning or use trim() function

$statement->bindValue('id', trim($id));
Sanjay Bharti
  • 166
  • 3
  • 5