This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Model; | |
class Product | |
{ | |
// etc... | |
} | |
class Customer | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Repository; | |
use \PDO; | |
class UserRepository | |
{ | |
private $connection; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class User | |
{ | |
public $id; | |
public $username; | |
public $firstname; | |
public $lastname; | |
public $email; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// assuming some query was completed | |
while (($data = $result->fetch()) !== false) { | |
echo $data['firstname'] . ' ' . $data['surname'] . "\n"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo $data[0]; // first column | |
echo $data['surname']; // column named 'surname' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 . ' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class UserRepository | |
{ | |
public function __construct($dao = null) | |
{ | |
if (!$dao) { | |
$dao = new DataAccessObject(); | |
} | |
$this->dao = $dao; | |
} |