class Database {
private $link;
public function __construct()
{
$this->connect();
}
private function connect() {
$config = require_once 'config/config.php';
$dsn = 'mysql:host='. $config['host'].';dbname='.$config['db_name'].';charset=' . $config['charset'];
$this->link = new PDO($dsn, $config['user_name'], $config['password']);
return $this;
}
public function execute($sql) {
$sth = $this->link->prepare($sql);
return $sth->execute();
}
public function query($sql) {
$exe = $this->execute($sql);
print_r($exe);
$result = $exe->fetchAll(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
if ($result === false) {
return [];
}
return $result;
}
}
$db = new Database();
$db->execute('INSERT INTO `user` SET `username`=\'Alex\',`password`=\'222222\', `date`=\'111\'' );