Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP] Błąd połączenia PDO (Programowanie obiektowe)
Kerth
post
Post #1





Grupa: Zarejestrowani
Postów: 250
Pomógł: 1
Dołączył: 6.08.2012

Ostrzeżenie: (0%)
-----


Cześć,
próbuję zrozumieć co jest źle w poniższym kodzie, że pojawiaja się ostrzeżenia. Co robię źle?
  1. class __construct{
  2. private $host = "localhost";
  3. private $username = "root";
  4. private $password = "pass";
  5. private $database = "database";
  6.  
  7. public function SQLConnection($host, $username, $password, $database){
  8. try{
  9. $db = new PDO('mysql:host='.$host.';dbname='.$database.'', ''.$username.'', ''.$password.'',array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
  10. echo 'Połączenie nawiązane!';
  11. }catch(PDOException $e){
  12. echo 'Połączenie nie mogło zostać utworzone.';
  13. }
  14.  
  15. }


Kod
Warning: Missing argument 1 for __construct::SQLConnection(), called in C:\WebServ\httpd\oop_mvc\index.php on line 12 and defined in C:\WebServ\httpd\oop_mvc\controller\ConfigDatabase.class.php on line 18

Warning: Missing argument 2 for __construct::SQLConnection(), called in C:\WebServ\httpd\oop_mvc\index.php on line 12 and defined in C:\WebServ\httpd\oop_mvc\controller\ConfigDatabase.class.php on line 18

Warning: Missing argument 3 for __construct::SQLConnection(), called in C:\WebServ\httpd\oop_mvc\index.php on line 12 and defined in C:\WebServ\httpd\oop_mvc\controller\ConfigDatabase.class.php on line 18

Warning: Missing argument 4 for __construct::SQLConnection(), called in C:\WebServ\httpd\oop_mvc\index.php on line 12 and defined in C:\WebServ\httpd\oop_mvc\controller\ConfigDatabase.class.php on line 18
Połączenie nawiązane!
Fatal error: Call to a member function query() on a non-object in C:\WebServ\httpd\oop_mvc\controller\ConfigDatabase.class.php on line 28
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Boshi
post
Post #2





Grupa: Zarejestrowani
Postów: 342
Pomógł: 15
Dołączył: 30.08.2011

Ostrzeżenie: (0%)
-----


Dane są z mojej bazy
  1. class Database{
  2.  
  3. private $db;
  4.  
  5. public function __construct($host, $database, $username,$pass )
  6. {
  7.  
  8. try
  9. {
  10. $this->db = new PDO('mysql:host='.$host.';dbname='.$database, $username,$pass,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
  11. echo 'Połączenie nawiązane!';
  12. }
  13. catch(PDOException $e)
  14. {
  15. echo 'Połączenie nie mogło zostać utworzone.'.$e->getMessage();
  16. }
  17.  
  18. }
  19.  
  20. public function getData()
  21. {
  22. $getData=$this->db->query('Select * From uzytkownicy');
  23.  
  24. foreach ($getData as $value)
  25. {
  26. echo $value['login']."<br>";
  27. }
  28.  
  29.  
  30. }
  31.  
  32. }
  33.  
  34. $ob=new Database('localhost','user','root', '');
  35. $ob->getData();
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 12.10.2025 - 20:05