Safely, and easily, allows you to create a PDO Skeleton.
Copyright 2018 Kyle Jeynes @ Daniel Pickering, All Rights Reserved.
By default, PDO::ATTR_EMULATE_PREPARES is set to true which may cause vulnerabilities in your web application. Open the DriverContainer.php file and search for PDO::ATTR_EMULATE_PREPARES
. Set it to false if not needed.
Continue to read on documentation:
/* Require the compulsory files */
require_once 'DriverContainer.php';
require_once 'DriverContainerOptions.php';
/* Use the namespace */
use \PDOEasy;
/* Instance \PDOEasy\DriverContainerOptions */
$options = new DriverContainerOptions(
'username',
'password',
'database_name'
'localhost'
);
/* Require the compulsory files */
require_once 'DriverContainer.php';
require_once 'DriverContainerOptions.php';
/* Use the namespace */
use \PDOEasy;
/* Instance \PDOEasy\DriverContainerOptions */
$options = new DriverContainerOptions(
'username',
'password',
'database_name',
'localhost',
'utf8mb4',
'3306',
'mysql'
);
final class MyFirstModel extends DriverContainer
{
public function doesExist($myValue) {
return (int) $this->query('SELECT 1 FROM myTable WHERE myRow = ?', [$myValue])->fetchColumn();
}
}
DriverContainer::setOptions(new DriverContainerOptions(
'username',
'password',
'database_name'
'localhost'
));
echo MyFirstModel::getInstance()->doesExist('some context') ? "It exists" : "It doesn't exist";
DriverContainer::flush(); // close connections to all Models instanced through getInstance()
\PDOEasy\DriverContainer::getSingleRow($sql, $values = [])
- Returns a single row of results from the query.
\PDOEasy\DriverContainer::getMultipleRows($sql, $values = [])
- Returns a multiple row of results from the query.
\PDOEasy\DriverContainer::query($sql, $values = [])
- Directly recieve the \PDOStatement after \PDOStatement::execute([]).
\PDOEasy\DriverContainer::doesExist($sql, $values = [])
- Check that rows do exists.
\PDOEasy\DriverContainer::getEntity()
- Advanced use only, returns the \PDO API instance.
final class MyFirstModel extends DriverContainer
{
private $user;
public function getUserById($unid) {
$this->user = new MyUser( $this->getSingleRow('SELECT unid, name, email FROM myUserTable WHERE unid = ?', [(int) $unid]) );
}
public function getUser() { return $this->user; }
public function removeUser($user) {
if(!$user instanceof MyUser)
throw new Exception("Call to MyFirstModel::insertUser - param expected to be of type MyUser");
$this->query('DELETE FROM myUserTable WHERE unid = ? AND email = ?)', [(int) $user->getUniqueNumberId(), $user->getEmail()]);
}
}
DriverContainer::setOptions(new DriverContainerOptions(
'username',
'password',
'database_name'
'localhost'
));
MyFirstModel::getInstance()->getUserById ('1');
MyFirstModel::getInstance()->getUserById (2);
MyFirstModel::getInstance()->removeUser(MyFirstModel::getInstance()->getUser());
DriverContainer::flush();