Guest User

Untitled

a guest
Aug 24th, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. PDO Connection to DB issues
  2. class DBConnector extends PDO
  3. {
  4. private $host;
  5. private $username;
  6. private $password;
  7. private $db;
  8. private $dns;
  9.  
  10. public function __construct($host, $username, $password, $db)
  11. {
  12. $this->host = $host;
  13. $this->username = $username;
  14. $this->password = $password;
  15. $this->db = $db;
  16.  
  17. $this->dns = "mysql:dbname=".$this->db.";host=".$host;
  18. $connection = parent::__construct($this->dns, $this->username, $this->password);
  19.  
  20. }
  21. }
  22.  
  23. function testQuery()
  24. {
  25. global $connection;
  26.  
  27. $query = "
  28. SELECT * FROM users
  29. ";
  30.  
  31. $stmt = $connection->prepare($query);
  32.  
  33. $result = $stmt->fetchAll();
  34.  
  35.  
  36. }
  37.  
  38. class DBConnector extends PDO
  39. {
  40. private $connection;
  41. private $host;
  42. private $username;
  43. private $password;
  44. private $db;
  45. private $dns;
  46.  
  47. public function __construct($host, $username, $password, $db)
  48. {
  49. $this->host = $host;
  50. $this->username = $username;
  51. $this->password = $password;
  52. $this->db = $db;
  53. $this->dns = "mysql:dbname=".$this->db.";host=".$host;
  54. $this->connection = parent::__construct($this->dns, $this->username, $this->password);
  55. $this->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  56. }
  57.  
  58. public function testQuery()
  59. {
  60. $query = "SELECT * FROM a";
  61. $stmt = $this->prepare($query);
  62. if($stmt->execute()){
  63. return $stmt->fetchAll();
  64. }
  65. return array();
  66. }
  67. }
  68.  
  69. $tg = new DBConnector('localhost', 'root', '', 'test');
  70. $t = $tg->testQuery();
  71. print_r($t);
  72.  
  73. function testQuery()
  74. {
  75. global $connection;
  76.  
  77. $query = "
  78. SELECT * FROM users
  79. ";
  80.  
  81. $stmt = $connection->prepare($query);
  82.  
  83. if($stmt->execute()){
  84.  
  85. $result = $stmt->fetchAll();
  86.  
  87. }
  88. }
Add Comment
Please, Sign In to add comment