/** * Returns a PDO instance * @param array $mysqlConfig * @return PDO */ public static function connect($mysqlConfig) { $dbConfig = array('dsn' => 'mysql:dbname=' . $mysqlConfig['dbname'] . ';host=' . $mysqlConfig['host'], 'user' => $mysqlConfig['user'], 'pass' => $mysqlConfig['pass'], 'options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); return parent::connect($dbConfig); }
<?php /** * Demo general DB * * @author Leonard Shtika <*****@*****.**> * @link http://leonard.shtika.info * @copyright (C) Leonard Shtika * @license MIT. See the file LICENSE for copying permission. */ require_once 'vendor/autoload.php'; use leoshtika\libs\DB; $config = array('dsn' => 'mysql:dbname=myapp;host=localhost', 'user' => 'root', 'pass' => ''); $dbh = DB::connect($config); $sth = $dbh->prepare('SELECT * FROM user'); $sth->execute(); $users = $sth->fetchAll(PDO::FETCH_ASSOC); foreach ($users as $user) { echo $user['name'] . ' Email: ' . $user['email']; echo '<hr>'; }