Using raw PDO MySQL queries with safe placeholders in Symfony

This comes in handy as Doctrine doesn’t always support all native MySQL commands and functions.  Strangely, certain common time and date-related ones like YEAR(), MONTH() and DAY().

The $paramsArr is an array for all placeholders in your standard PDO query.

// Symfony raw queries with placeholders.
// Example query:
$query = "SELECT * FROM users WHERE status=:status
              GROUP BY last_name ORDER BY last_name ASC";
$paramsArr=array('status'=>'Active');
$stmt = $this->em->getConnection()->prepare($query);
$stmt->execute($paramsArr);
$resultsArr=$stmt->fetchAll();

About Author:

Senior Cloud Software Engineer and 25+ years experienced video production, video editing and 3D animation services for a variety of global clients including local video production here in Jacksonville, Florida.

Leave a Comment

Your email address will not be published. Required fields are marked *