Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Database
- {
- /**
- * @var
- */
- private $link;
- /**
- * Database constructor.
- */
- public function __construct()
- {
- $this->connect();
- }
- /**
- * @return $this
- */
- private function connect()
- {
- $config = require_once 'config.php';
- $dsn = 'mysql:host='.$config['host'].';db_name='.$config['db_name'].';charset='.$config['charset'];
- $this->link = new PDO($dsn, $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)
- {
- $sth = $this->link->prepare($sql);
- $sth->execute();
- $result = $sth->fetchAll(PDO::FETCH_ASSOC);
- if($result === false){
- return [];
- }
- return $result;
- }
- }
- $db = new Database();
- $db->execute("INSERT INTO `user` SET `username`='Pavel', `password`='444444', `date`=".time());
Add Comment
Please, Sign In to add comment