The Helper class provides utility functions for development and debugging. Currently, it includes a PDO query debugger.Located at: application/libs/helper.php
class Helper{ /** * debugPDO * * Shows the emulated SQL query in a PDO statement. * Combines the raw query and the placeholders. * * @author Panique * @param string $raw_sql * @param array $parameters * @return string */ static public function debugPDO($raw_sql, $parameters) { // ... implementation }}
$sql = "SELECT id, artist, track FROM song WHERE id = :song_id LIMIT 1";$query = $this->db->prepare($sql);$parameters = array(':song_id' => 5);echo Helper::debugPDO($sql, $parameters);// Output: SELECT id, artist, track FROM song WHERE id = '5' LIMIT 1$query->execute($parameters);
$sql = "UPDATE song SET artist = :artist, track = :track WHERE id = :song_id";$parameters = array( ':artist' => 'Pink Floyd', ':track' => 'Comfortably Numb', ':song_id' => 17);echo Helper::debugPDO($sql, $parameters);// Output: UPDATE song SET artist = 'Pink Floyd', track = 'Comfortably Numb' WHERE id = '17'
$sql = "SELECT * FROM song WHERE artist = ? AND track = ?";$parameters = array('Queen', 'Bohemian Rhapsody');echo Helper::debugPDO($sql, $parameters);// Output: SELECT * FROM song WHERE artist = 'Queen' AND track = 'Bohemian Rhapsody'
Add debugging to your model methods during development:
public function getSong($song_id){ $sql = "SELECT id, artist, track, link FROM song WHERE id = :song_id LIMIT 1"; $query = $this->db->prepare($sql); $parameters = array(':song_id' => $song_id); // Debug during development echo '[ PDO DEBUG ]: ' . Helper::debugPDO($sql, $parameters); exit(); // Stop execution to see the debug output $query->execute($parameters); return $query->fetch();}
Remove or comment out Helper::debugPDO() calls in production code. They’re meant for development debugging only.
// Good for development:if (ENVIRONMENT == 'development') { echo Helper::debugPDO($sql, $parameters);}// Or use a debug flag:if (defined('DEBUG') && DEBUG === true) { echo Helper::debugPDO($sql, $parameters);}
Combine with exit() for Debugging
Stop execution after debug output to focus on the query:
public function searchSongs($artist, $track, $min_id){ $sql = "SELECT id, artist, track, link FROM song WHERE artist LIKE :artist AND track LIKE :track AND id > :min_id ORDER BY id DESC LIMIT 20"; $parameters = array( ':artist' => '%' . $artist . '%', ':track' => '%' . $track . '%', ':min_id' => $min_id ); // Debug to see the actual query echo '<pre>' . Helper::debugPDO($sql, $parameters) . '</pre>'; // Output: // SELECT id, artist, track, link // FROM song // WHERE artist LIKE '%Beatles%' // AND track LIKE '%Hey%' // AND id > '10' // ORDER BY id DESC // LIMIT 20 $query = $this->db->prepare($sql); $query->execute($parameters); return $query->fetchAll();}
This shows you exactly what LIKE patterns and values are being used, making it easy to spot issues with wildcards or empty parameters.