return array(
'host' => 'localhost',
'db_name' => 'test_pdo',
'username' => 'root',
'password' => '',
'charset' => 'utf8'
);
class Connection
{
private $link;
/**
* Connection constructor.
*/
public function __construct(){
$this->connect();
}
/**
* @return $this
*/
private function connect(){
$config = require_once 'config.php';
$dns = 'myslqi:host='.$config['host'].';dbname'.$config['db_name'].';charset'.$config['charset'];
$this->link = new PDO($dns, $config['username'], $config['password']);
return $this;
}
/**
* @param $sql
* @return mixed
*/
public function execute($sql){
$sth = $this->link->prepare($sql);
return $sth->execute();
}
/**
* @param $sql
* @return array
*/
public function query($sql){
$exe = $this->execute($sql);
$result = $exe->fetchALL(PDO::FETCH_ASSOC);
if($result === false){
return[];
}
return $result;
}
}
$db = new Connection();