Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PDO Connection to DB issues
- class DBConnector extends PDO
- {
- private $host;
- private $username;
- private $password;
- private $db;
- private $dns;
- public function __construct($host, $username, $password, $db)
- {
- $this->host = $host;
- $this->username = $username;
- $this->password = $password;
- $this->db = $db;
- $this->dns = "mysql:dbname=".$this->db.";host=".$host;
- $connection = parent::__construct($this->dns, $this->username, $this->password);
- }
- }
- function testQuery()
- {
- global $connection;
- $query = "
- SELECT * FROM users
- ";
- $stmt = $connection->prepare($query);
- $result = $stmt->fetchAll();
- }
- class DBConnector extends PDO
- {
- private $connection;
- private $host;
- private $username;
- private $password;
- private $db;
- private $dns;
- public function __construct($host, $username, $password, $db)
- {
- $this->host = $host;
- $this->username = $username;
- $this->password = $password;
- $this->db = $db;
- $this->dns = "mysql:dbname=".$this->db.";host=".$host;
- $this->connection = parent::__construct($this->dns, $this->username, $this->password);
- $this->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- public function testQuery()
- {
- $query = "SELECT * FROM a";
- $stmt = $this->prepare($query);
- if($stmt->execute()){
- return $stmt->fetchAll();
- }
- return array();
- }
- }
- $tg = new DBConnector('localhost', 'root', '', 'test');
- $t = $tg->testQuery();
- print_r($t);
- function testQuery()
- {
- global $connection;
- $query = "
- SELECT * FROM users
- ";
- $stmt = $connection->prepare($query);
- if($stmt->execute()){
- $result = $stmt->fetchAll();
- }
- }
Add Comment
Please, Sign In to add comment