Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function prepareExecFetch() { ... }
- public function prepareExec() { ... }
- public function fetch() { ... }
- $stmt1 = $pdo->prepare('...');
- $stmt1->execute(array('...' => '...'));
- $r1 = $stmt1->fetch(PDO::FETCH_ASSOC);
- <?php
- class DB {
- private $host;
- private $name;
- private $charset;
- private $user;
- private $password;
- // I wonder how could I store all the statements here, using the
- // $stmt1 = ...; $stmt2 = ...; wouldn't work cause it has to be
- // dymanic and I can't create new properties inside any method
- private $stmts = [];
- // Not using static modifier because if there was another instance of this
- // class, then $stmtCounter variable should be resetted again to 0
- private $stmtCounter = 0;
- public function __construct(string $host, string $name, string $charset,
- string $user, string $password) {
- $this->host = $host;
- $this->name = $name;
- $this->charset = $charset;
- $this->user = $user;
- $this->password = $password;
- $pdo = new PDO('mysql:host=' . $this->host . '; dbname=' . $this->name . ';
- charset=' . $this->charset, $this->user, $this->password);
- // While false, PDO will take advantage of the native prepared statement
- // functionality
- $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
- // In exception mode, if there is an error in SQL, PDO will throw
- // exceptions and the script will stop running
- $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- public function execPrepareFetch(string $stmt, array $execParams) {
- $this->stmtCounter++;
- array_push($this->stmts, $stmt . $this->stmtCounter);
- }
- }
- // This way by instantiating multiple instances we can easily work with multiple
- //databases
- $db = new DB(DB_HOST, DB_NAME, DB_CHARSET, DB_USER, DB_PASSWORD);
- $db->execPrepareFetch('$stmt', [':user' => 'some value']);
Add Comment
Please, Sign In to add comment