Created
May 8, 2017 14:18
-
-
Save Tjoosten/5c85c5d314e760deb7aa470de2830077 to your computer and use it in GitHub Desktop.
This file contains 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 ActivismeBe\Dataset\Adapters; | |
use PDO; | |
use PDOException; | |
use Dotenv\Dotenv; | |
/** | |
* Class MysqlAdapter | |
* | |
* @package ActivismeBe\Dataset\Adapters | |
*/ | |
class MysqlAdapter | |
{ | |
/** | |
* Provided variable for the database connection. | |
* @var PDO | |
*/ | |
private $dbh; | |
/** | |
* Provided variable for pdo errors | |
* | |
* @var string | |
*/ | |
private $error; | |
/** | |
* Provided variable for the queries. | |
* | |
* @var $stmt | |
*/ | |
private $stmt; | |
/** | |
* MysqlAdapter constructor. | |
* | |
* @return MySQL database connection | |
* @throws \PDOException | |
*/ | |
public function __construct() | |
{ | |
$dotenv = new Dotenv(__DIR__ . '/../../'); | |
$dotenv->overload(); | |
$dsn = 'mysql:host=' . getenv('DB_HOST') . ';dbname=' . getenv('DB_NAME'); | |
$options = [PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]; | |
try { // Create a new PDO instance | |
$this->dbh = new PDO($dsn, getenv('DB_USER'), getenv('DB_PASS'), $options); | |
} catch (PDOException $e) { // Catch any errors | |
$this->error = $e->getMessage(); | |
} | |
} | |
/** | |
* [Prepare] | |
* | |
* The query method also introduces the PDO:prepare statement. | |
* | |
* The Prepare function allows you to vind values in your SQL Statements. This | |
* is important because it take away the threat of SQL Injection vecause you are | |
* no longer having manually include the parameters into the query string. | |
* | |
* Using the prepare function will also improve performance when rynning the | |
* same query with different parameters multiple times. | |
* | |
* @param string $query The database query. | |
* @return void | |
*/ | |
public function query($query) | |
{ | |
$this->stmt = $this->dbh->prepare($query); | |
} | |
/** | |
* [Bind] | |
* | |
* The next method we will be looking at is the bind method. In order to prepare | |
* our SQL queries, we need to bind the inputs with the placeholders we put in | |
* place. This is what the Bind method used for. | |
* | |
* The main part of this method is based upon the PDOStatement::bindValue PDO | |
* method. | |
* | |
* @param string $param Is the actual placeholder value that we will be using in out SQL statement. | |
* @param string $value Is the actual param taht we want to bind to the placeholder. | |
* @param mixed $type Is the datatype of the parameter, example string. default = null | |
* @return mixed | |
*/ | |
public function bind($param, $value, $type = null) | |
{ | |
if (is_null($type)) { | |
switch(true) { | |
case is_int($value): | |
$type = PDO::PARAM_INT; | |
break; | |
case is_bool($value): | |
$type = PDO::PARAM_BOOL; | |
break; | |
case is_null($value): | |
$type = PDO::PARAM_NULL; | |
break; | |
default: | |
$type = PDO::PARAM_STR; | |
} | |
} | |
$this->stmt->bindValue($param, $value, $type); | |
} | |
/** | |
* [Execute] | |
* | |
* The next method we will be look at is the PDOStatement::execute. The execute | |
* method executes the prepared statement. | |
*/ | |
public function execute() | |
{ | |
return $this->stmt->execute(); | |
} | |
} | |
http://culttt.com/2012/10/01/roll-your-own-pdo-php-class/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment