Guest User

Untitled

a guest
Sep 3rd, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. php pdo mysql connect - confusing syntax problem
  2. <?php
  3. $mysql_host='mysql1.000webhost.com';
  4. $mysql_dbname='a8130617_skola';
  5. $mysql_username='something';
  6. $mysql_password='something';
  7.  
  8. class mysql {
  9. try{
  10. public $db = new PDO("mysql:host=$mysql_host;dbname=$mysql_dbname",
  11. $mysql_username, $mysql_password);
  12. }
  13. catch(PDOException $e){
  14. echo $e->getMessage();
  15. }
  16. } //ERROR EXCLAMATION MARK HERE???
  17. ?>
  18.  
  19. try{
  20. public $db = new PDO("mysql:host=$mysql_host;dbname=$mysql_dbname",
  21. $mysql_username, $mysql_password);
  22. }
  23. catch(PDOException $e){
  24. echo $e->getMessage();
  25. }
  26.  
  27. class Mysql {
  28.  
  29. protected $_host;
  30. protected $_dbname;
  31. protected $_username;
  32. protected $_password;
  33. protected $_db;
  34.  
  35. public function __construct($host = null, $dbname = null, $username = null, $password = null)
  36. {
  37. $this->_host = $host;
  38. $this->_dbname = $dbname;
  39. $this->_username = $username;
  40. $this->_password = $password;
  41. }
  42.  
  43. public function connect()
  44. {
  45. try {
  46. $this->_db = new PDO('mysql:host=' . $this->_host . ';dbname=' . $this->_dbname, $this->_username, $this->_password);
  47. }
  48. catch(PDOException $e){
  49. echo $e->getMessage();
  50. }
  51. }
  52.  
  53. public function getDb()
  54. {
  55. return $this->db;
  56. }
  57.  
  58. public function setHost($host)
  59. {
  60. $this->_host = $host;
  61. return $this;
  62. }
  63.  
  64. public function getHost()
  65. {
  66. return $this->_host;
  67. }
  68.  
  69. public function setDbname($dbname)
  70. {
  71. $this->_dbname = $dbname;
  72. return $this;
  73. }
  74.  
  75. public function getDbname()
  76. {
  77. return $this->_dbname;
  78. }
  79.  
  80. public function setUsername($username)
  81. {
  82. $this->_username = $username;
  83. return $this;
  84. }
  85.  
  86. public function getUsername()
  87. {
  88. return $this->_username;
  89. }
  90.  
  91. public function setPassword($password)
  92. {
  93. $this->_password = $password;
  94. return $this;
  95. }
  96.  
  97. public function getPassword()
  98. {
  99. return $this->_password;
  100. }
  101.  
  102.  
  103. }
  104.  
  105. $mysql = new Mysql('mysql1.000webhost.com', 'a8130617_skola', 'something', 'something');
  106. $mysql->connect();
Add Comment
Please, Sign In to add comment