Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP][OOP] Błąd z prepare
Dominator
post 9.05.2013, 13:56:01
Post #1





Grupa: Zarejestrowani
Postów: 565
Pomógł: 15
Dołączył: 11.10.2010

Ostrzeżenie: (20%)
X----


  1. Class User
  2. {
  3. public function getUsername($id)
  4. {
  5. try
  6. {
  7. $pdo = new DB("","","","","");
  8. $sqlprep = $pdo->prepare("XX");
  9. $sqlprep->execute();
  10. }
  11. catch(PDOException $e)
  12. {
  13. echo "Error! Code ->".$e->getMessage();
  14. }
  15. }
  16. }
  17.  
  18. $usr = new User();
  19. $usr -> getUsername("65");


Taki jest błąd: Fatal error: Call to undefined method DB::prepare() in
Co zrobiłem źle?

---
Tak wygląda klasa do łączenia się z bazą:
  1. Class DB
  2. {
  3. protected $server;
  4. protected $port ;
  5. protected $user;
  6. protected $password;
  7. protected $database;
  8. protected $db;
  9. public function __construct($server, $port, $username, $password, $database)
  10. {
  11. $this->Set($server, $port, $username, $password, $database);
  12. $this->Connect();
  13. }
  14. protected function Set($server, $port, $username, $password, $database)
  15. {
  16. $this->server = $server;
  17. $this->port = $port;
  18. $this->user = $username;
  19. $this->password = $password;
  20. $this->database = $database;
  21. }
  22. protected function Connect()
  23. {
  24. $pdo = new PDO('mysql:host='.$this->server.';dbname='.$this->database.';port='.$this->port, $this->user, $this->password );
  25. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  26. }
  27. }
Go to the top of the page
+Quote Post
Tajgeer
post 9.05.2013, 13:57:38
Post #2





Grupa: Zarejestrowani
Postów: 290
Pomógł: 48
Dołączył: 11.05.2008
Skąd: Kielce

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


Spraw, aby klasa DB dziedziczyła (rozszerzała) od PDO. Bez tego nie będziesz miał dostępnych metod, które są w PDO w swojej klasie.

Ten post edytował Tajgeer 9.05.2013, 13:58:00
Go to the top of the page
+Quote Post
Dominator
post 9.05.2013, 14:00:11
Post #3





Grupa: Zarejestrowani
Postów: 565
Pomógł: 15
Dołączył: 11.10.2010

Ostrzeżenie: (20%)
X----


Class DB extends PDO ?
Go to the top of the page
+Quote Post
skowron-line
post 9.05.2013, 14:09:42
Post #4





Grupa: Zarejestrowani
Postów: 4 340
Pomógł: 542
Dołączył: 15.01.2006
Skąd: Olsztyn/Warszawa

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


Cytat(Dominator @ 9.05.2013, 14:00:11 ) *
Class DB extends PDO ?

Tak, albo poczytaj o wstrzykiwaniu zależności


--------------------
I'm so fast that last night I turned off the light switch in my hotel room and was in bed before the room was dark - Muhammad Ali.
Peg jeżeli chcesz uprawiać sex to dzieci muszą wyjść, a jeżeli chcesz żeby był dobry ty też musisz wyjść - Al Bundy.

QueryBuilder, Mootools.net, bbcradio1::MistaJam
http://www.phpbench.com/
Go to the top of the page
+Quote Post
ber32
post 9.05.2013, 14:46:12
Post #5





Grupa: Zarejestrowani
Postów: 332
Pomógł: 22
Dołączył: 6.07.2010

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


Tu masz przykład Połączenie z bazą danych


--------------------
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 24.07.2025 - 16:32