Skip to content

Instantly share code, notes, and snippets.

View coreymcmahon's full-sized avatar

Corey McMahon coreymcmahon

View GitHub Profile
<?php
namespace Model;
class Product
{
// etc...
}
class Customer
{
@coreymcmahon
coreymcmahon / PDOPreparedStatements.php
Created February 9, 2013 11:55
Using prepared statements with PDO. From the article: PDO for Elegant PHP Database Access, http://www.modernphpbook.com/articles/pdo-for-elegant-php-database-access - Fig 10
<?php
$conn = new PDO('mysql:host=localhost;dbname=someDb', $username, $password);
$stmt = $conn->prepare ('
INSERT INTO user (firstname, surname) VALUES (:firstname, :surname)
');
$stmt -> bindParam(':firstname', 'John');
$stmt -> bindParam(':surname', 'Smith');
@coreymcmahon
coreymcmahon / UserRepository.php
Last active October 30, 2016 18:37
Our User repository, version 2.0. From the article: PDO for Elegant PHP Database Access, http://www.modernphpbook.com/articles/pdo-for-elegant-php-database-access - Fig 12
<?php
namespace Repository;
use \PDO;
class UserRepository
{
private $connection;
@coreymcmahon
coreymcmahon / User.php
Created February 9, 2013 11:43
Our User model, version 2.0. From the article: PDO for Elegant PHP Database Access, http://www.modernphpbook.com/articles/pdo-for-elegant-php-database-access - Fig 11
<?php
class User
{
public $id;
public $username;
public $firstname;
public $lastname;
public $email;
@coreymcmahon
coreymcmahon / PDOExample.php
Created February 7, 2013 11:21
Using PDO for database access - handling errors. From the article: PDO for Elegant PHP Database Access, http://www.modernphpbook.com/articles/pdo-for-elegant-php-database-access - Fig 9
<?php
try {
$conn = new PDO('mysql:host=localhost;dbname=someDb', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// run queries...
} catch (PDOException $ex) {
echo "Error: " . $e->getMessage();
}
@coreymcmahon
coreymcmahon / PDOExample.php
Created February 7, 2013 11:20
Using PDO for database access - accessing rows. From the article: PDO for Elegant PHP Database Access, http://www.modernphpbook.com/articles/pdo-for-elegant-php-database-access - Fig 8
<?php
// assuming some query was completed
while (($data = $result->fetch()) !== false) {
echo $data['firstname'] . ' ' . $data['surname'] . "\n";
}
@coreymcmahon
coreymcmahon / PDOExample.php
Created February 7, 2013 11:19
Using PDO for database access - accessing a 'fetched' row with PDO::FETCH_BOTH. From the article: PDO for Elegant PHP Database Access, http://www.modernphpbook.com/articles/pdo-for-elegant-php-database-access - Fig 7
<?php
echo $data[0]; // first column
echo $data['surname']; // column named 'surname'
@coreymcmahon
coreymcmahon / PDOExample.php
Created February 7, 2013 11:17
Using PDO for database access. From the article: PDO for Elegant PHP Database Access, http://www.modernphpbook.com/articles/pdo-for-elegant-php-database-access - Fig 6
<?php
$conn = new PDO('mysql:host=localhost;dbname=someDb', $username, $password);
$result = $conn->query('
SELECT * FROM myTable WHERE name = ' . $conn->quote($name) . '
');
$data = $query->fetch();
@coreymcmahon
coreymcmahon / UserRepository.php
Created February 7, 2013 11:14
A model implementation using the repository pattern and entity caching. From the article: PDO for Elegant PHP Database Access, http://www.modernphpbook.com/articles/pdo-for-elegant-php-database-access - Fig 5
<?php
class UserRepository
{
private $userCache = array();
public function find($id)
{
if (!isset($this->userCache[$id])) {
$userCache[$id] = $this->dao->query('
SELECT * FROM users WHERE id = ' . (int)$id . '
@coreymcmahon
coreymcmahon / UserRepository.php
Created February 7, 2013 11:13
A model implementation using the repository pattern. From the article: PDO for Elegant PHP Database Access, http://www.modernphpbook.com/articles/pdo-for-elegant-php-database-access - Fig 4
<?php
class UserRepository
{
public function __construct($dao = null)
{
if (!$dao) {
$dao = new DataAccessObject();
}
$this->dao = $dao;
}