PdoHelper

Last updated: August 12th, 2023
Release:

The PdoHelper class is a wrapper for PHP's PDO class that adds several pieces of functionality:

  • Better error logging
  • Query logging/meta information
  • Better connected/disconnected display
  • Stored queries

The logging/meta information improvements are in place just by using the PdoHelper class. In order to view them you can use the following methods:

public PdoDrivers getDriver() Returns the currently configured PDO driver public PdoError[] getErrors() Returns all previous PDOExceptions with their triggering queries public PdoQuery[] getQueries() Returns all queries that have been run through the helper public integer getQueryCount() Returns the number of queries that have been run through the helper

Stored Queries

Stored queries are a way to re-use queries (per database engine) without any extra code. As a quick example, let's create some for both MySQL and MSSQL:


using Stoic\Pdo\PdoDrivers;
using Stoic\Pdo\PdoHelper;

PdoHelper::storeQuery(
	PdoDrivers::PDO_MSSQL,
	'sel-user-email',
	"SELECT [Email] FROM [dbo].[User] WHERE [ID] = :id",
	[':id' => \PDO::PARAM_INT]);
PdoHelper::storeQuery(
	PdoDrivers::PDO_MYSQL,
	'sel-user-email',
	"SELECT `Email` FROM `User` WHERE `ID` = :id",
	[':id' => \PDO::PARAM_INT]);

$db = new PdoHelper('mysql:dbname=testdb;host=127.0.0.1');
$stmt = $db->prepareStored('sel-user-email', [':id' => 1]); // calls up the MySQL version
$stmt->execute();                                           // because of the connection's driver

Next Up

Continue to read about the web component, or visit the Table of Contents.