Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL][PHP]Klasy i obiekty
Forum PHP.pl > Forum > Przedszkole
elldiablo
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>';
Mephistofeles
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.
sazian
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
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.