Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [MySQL][PHP]Klasy i obiekty, nauka
elldiablo
post 17.12.2010, 19:42:59
Post #1





Grupa: Zarejestrowani
Postów: 79
Pomógł: 1
Dołączył: 30.05.2009

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


Witam,
Utworzyłem klasę która łączy się z bazą i pobieram dane z bazy. Do utworzenie nowej klasy tj. "shop" pobieram dane z bazy i var_dump() zwraca mi dane. Jest wszystko OK.
Problem pojawia się gdy tworzę nową klasę i przez nią chcę pobrać dane z bazy, czyli klasa "shop".
Chciałbym aby klasa "shop" pobierała dane. Niestety zwraca mi "NULL".
Co robię nie tak?


  1. define (_HOST_, 'localhost');
  2. define (_USER_, 'root');
  3. define (_PASSWORD_, 'admin');
  4. define (_DATABASE_, 'test');
  5.  
  6. class DB {
  7. protected $_host;
  8. protected $_user;
  9. protected $_password;
  10. protected $_database;
  11. public $_connect;
  12. public $_result;
  13. public $_row;
  14.  
  15. public function __construct() {
  16. $this->_host = _HOST_;
  17. $this->_user = _USER_;
  18. $this->_password = _PASSWORD_;
  19. $this->_database = _DATABASE_;
  20. $this->_connect = $_connect;
  21. $this->_result = $_result;
  22. $this->_row = $_row;
  23. }
  24.  
  25. public function connect() {
  26. if($this->_connect = mysql_connect($this->_host, $this->_user, $this->_password)) {
  27. if(!$this->_setDB($this->_database)) die ('Nie można odnaleźć bazy danych');
  28. } else {
  29. return 'Nie można nawiązać połączenia z bazą danych';
  30. }
  31.  
  32. }
  33.  
  34. public function _setDB($db_name) {
  35. return mysql_select_db ($db_name, $this->_connect);
  36. }
  37.  
  38. public function Execute($query) {
  39. $this->_result = false;
  40. if($this->_connect) {
  41. if($this->_result = mysql_query($query)) {
  42. while ($this->_row = mysql_fetch_assoc($this->_result))
  43. $resultArray[] = $this->_row;
  44. return $resultArray;
  45. }
  46. }
  47. }
  48. }
  49. $db = new DB();
  50. $db->connect();
  51. // Tutaj pobieram dane z bazy - tablica
  52. //$result = $db->Execute("SELECT * FROM shop");
  53.  
  54. class shop {
  55. public $_query;
  56.  
  57. public function __construct() {
  58. $this->_query = $_query;
  59. }
  60.  
  61. public function shops() {
  62. $this->_query = DB::Execute("SELECT * FROM shop");
  63. return $this->_query;
  64. }
  65. }
  66.  
  67. $shop = new shop();
  68. $result = $shop->shops();
  69.  
  70. // Tutaj zwraca mi NULL.
  71. echo '<pre>';
  72. var_dump($result);
  73. echo '</pre>';
Go to the top of the page
+Quote Post
Mephistofeles
post 17.12.2010, 19:51:03
Post #2





Grupa: Zarejestrowani
Postów: 1 182
Pomógł: 115
Dołączył: 4.03.2009
Skąd: Myszków

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


To co robisz nie ma sensu.
Używasz klasy jako kontenera na funkcje, a to nie o to chodzi. Poza tym Twoja klasa jest niepotrzebna, skoro chcesz użyć OOP skorzystaj z PDO.
Go to the top of the page
+Quote Post
sazian
post 17.12.2010, 20:10:34
Post #3





Grupa: Zarejestrowani
Postów: 1 045
Pomógł: 141
Dołączył: 19.09.2006
Skąd: B-tów

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


ok, po kolei
konstruktor
  1. $this->_host = _HOST_;
  2. $this->_user = _USER_;
  3. $this->_password = _PASSWORD_;
  4. $this->_database = _DATABASE_;
  5. $this->_connect = $_connect;
  6. $this->_result = $_result;
  7. $this->_row = $_row;

1) nie chce mi się sprawdzać ale wydaje mi się że nie masz dostępu do tych stałych wewnątrz klasy
2) skąd bierzesz zmienne $_connect $_result i $_row?? to samo jest w klasie shop
  1. while ($this->_row = mysql_fetch_assoc($this->_result))
  2. $resultArray[] = $this->_row;
  3. return $resultArray;

a nie lepiej dać mysql_fetch_array i tą tablice wyrzucić questionmark.gif teraz przepisujesz jedną tablicę do drugiej i dopiero ją wywalasz
  1. public function shops() {
  2. $this->_query = DB::Execute("SELECT * FROM shop");
  3. return $this->_query;
  4. }



metoda Execute klasy DB nie jest statyczna

Ten post edytował sazian 17.12.2010, 20:11:54
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: 31.07.2025 - 10:40