Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP5][SQL]Brak wyników na stronie
Forum PHP.pl > Forum > PHP > Object-oriented programming
lDoran
Kod pochodzi "PHP5 Zaawansowane programowanie"(został podpięty pod MySQL) - w przeglądarce pojawia się błąd:
Cytat
Parse error: syntax error, unexpected ']', expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\php5\class.DataManager.php on line 128

WTF? Już nie wiem jak do tego podejść próbowałem z przodu i tyłem do przodu, ale nic nie buczy. sciana.gif
Poniżej umieszczam poprawiony przeze mnie kod:
  1. <?php
  2. require_once('class.Entity.php'); //potrzebne później
  3. require_once('class.Individual.php');
  4. require_once('class.Organization.php');
  5.  
  6. class DataManager
  7. {
  8. private static function _getConnection() {
  9. static $hDB;
  10.  
  11. if(isset($hDB)) {
  12. return $hDB;
  13. }
  14.  
  15. $hDB = mysqli_connect('localhost', 'root', 'cu9DQppl', 'rozd3')
  16. or die("Błąd połączenia z bazą danych!");
  17. return $hDB;
  18. }
  19.  
  20. public static function getAddressData($addressID) {
  21. $sql = "SELECT * FROM adres WHERE adres_id ='". $addressID."'";
  22. $res = mysqli_query(DataManager::_getConnection(), $sql);
  23. if(! ($res && mysqli_num_rows($res))) {
  24. die("Błąd odczytu danych dla adresu pocztowego'" . $addressID . "'");
  25. }
  26.  
  27. return mysqli_fetch_assoc($res);
  28. }
  29.  
  30. public static function getEmailData($emailID) {
  31.  
  32. $sql = "SELECT * FROM email WHERE email_id ='". $emailID ."'";
  33. $res = mysqli_query(DataManager::_getConnection(), $sql);
  34. if(! ($res && mysqli_num_rows($res))) {
  35. die("Błąd odczytu danych dla adresu email'" . $emailID . "'");
  36. }
  37.  
  38. return mysqli_fetch_assoc($res);
  39. }
  40.  
  41. public static function getPhoneNumberData($phoneID) {
  42.  
  43. $sql = "SELECT * FROM telefon WHERE telefon_id ='". $phoneID ."'";
  44. $res = mysqli_query(DataManager::_getConnection(), $sql);
  45. if(! ($res && mysqli_num_rows($res))) {
  46. die("Błąd odczytu danych dla numeru telefonu'" . $phoneID . "'");
  47. }
  48.  
  49. return mysqli_fetch_assoc($res);
  50. }
  51.  
  52. public static function getEntityData($entityID)
  53. {
  54. $sql = "SELECT * FROM jednostka WHERE jednostka_id = '". $entityID ."'";
  55. $res = mysqli_query(DataManager::_getConnection(),$sql);
  56. if(! ($res && mysqli_num_rows($res)))
  57. die("Błąd odczytu jednostki '" . $entityID . "'");
  58.  
  59. return mysqli_fetch_assoc($res);
  60. }
  61.  
  62. public static function getAddressObjectsForEntity($entityID) {
  63. $sql = "SELECT adres_id FROM adres WHERE jednostka_id ='". $entityID ."'";
  64. $res = mysqli_query(DataManager::_getConnection(), $sql);
  65.  
  66. if(!$res)
  67. die("Błąd odczytu adresów dla jednostki'" . $entityID . "'");
  68.  
  69. if(mysqli_num_rows($res))
  70. {
  71. $objs = array();
  72. while($rec = mysqli_fetch_assoc($res))
  73. $objs[] = new Address($rec['adres_id']);
  74.  
  75. return $objs;
  76. }
  77. else
  78. return array();
  79. }
  80.  
  81. public static function getEmailObjectsForEntity($entityID)
  82. {
  83. $sql = "SELECT email_id FROM email WHERE jednostka_id ='". $entityID ."'";
  84. $res = mysqli_query(DataManager::_getConnection(), $sql);
  85. if(!$res)
  86. die("Błąd odczytu adresów email dla jednostki'" . $entityID . "'");
  87.  
  88.  
  89. if(mysqli_num_rows($res))
  90. {
  91. $objs = array();
  92. while($rec = mysqli_fetch_assoc($res))
  93. $objs[] = new EmailAddress($rec['email_id']);
  94.  
  95. return $objs;
  96. }
  97. else
  98. return array();
  99. }
  100.  
  101. public static function getEmployer($individualID)
  102. {
  103. $sql = "SELECT organizacja_id FROM zatrudniony WHERE osoba_id ='". $individualID ."'";
  104. $res = mysqli_query(DataManager::_getConnection(),$sql);
  105. if(! ($res && mysqli_num_rows($res)))
  106. die("Błąd odczytu pracodawcy dla osoby '".$individualID."'");
  107.  
  108. $row = mysqli_fetch_assoc($res);
  109.  
  110. if($row)
  111. return new Organization($row['organizacja_id']);
  112. else
  113. return null;
  114. }
  115.  
  116. public static function getEmployees($orgID)
  117. {
  118. $sql = "SELECT osoba_id FROM zatrudniony WHERE organizacja_id = '". $orgID ."'";
  119. $res = mysqli_query(DataManager::_getConnection(), $sql);
  120.  
  121. if(! ($res && mysqli_num_rows($res)))
  122. die("Błąd odczytu informacji o zatrudnionych dla organizacji'" . $orgID .`"'");
  123.  
  124. if(mysqli_num_rows($res))
  125. {
  126. $objs = array();
  127. while($row = mysqli_fetch_assoc($res))
  128. $objs[] = new Individual($row['osoba_id']);
  129.  
  130. return $objs;
  131. }
  132. else
  133. return array();
  134. }
  135.  
  136. public static function getAllEntitiesAsObjects()
  137. {
  138. $sql = "SELECT jednostka_id, ctype FROM jednostka";
  139. $res = mysqli_query(DataManager::_getConnection(), $sql);
  140.  
  141. if(!$res)
  142. die("Bład pobierania wszystkich jednostek!");
  143.  
  144. if(mysqli_num_rows($res))
  145. {
  146. $objs = array();
  147. while($row = mysqli_fetch_assoc($res))
  148. {
  149. if($row['typ'] == '1')
  150. $objs[] = new Individual($row['jednostka_id']);
  151. elseif ($row['typ'] == '0')
  152. $objs[] = new Organization($row['jednostka_id']);
  153. else
  154. die("Nieznany typ jednostki" . $row['typ'] . "!");
  155. }
  156. return $objs;
  157. }
  158. else
  159. return array();
  160. }
  161. }
  162. ?>


skrypt testowy
  1. <?php
  2. require_once('class.DataManager.php'); // ten plik dołącza pozostałe
  3.  
  4. function println($data) {
  5. print $data . "<br>\n";
  6. }
  7.  
  8. $arContacts = DataManager::getAllEntitiesAsObjects();
  9. foreach($arContacts as $objEntity) {
  10. if(get_class($objEntity) == 'Individual') {
  11. print "<h1>Osoba -' {$objEntity->__toString()}</h1>";
  12. } else {
  13. print "<h1>Organizacja - {$objEntity->__toString()}</h1>";
  14. }
  15.  
  16. if($objEntity->getNumberOfEmails()) {
  17. // Posiada adresy email, wypisz nagłówek
  18. print "<h2>Adresy email</h2>";
  19.  
  20. for($x=0; $x < $objEntity->getNumberOfEmails(); $x++) {
  21. println($objEntity->emails($x)->__toString());
  22. }
  23. }
  24.  
  25. if($objEntity->getNumberOfAddresses()) {
  26. // Posiada adresy pocztowe
  27. print "<h2>Adresy pocztowe</h2>";
  28.  
  29. for($x=0; $x < $objEntity->getNumberOfAddresses(); $x++) {
  30. println($objEntity->addresses($x)->__toString());
  31. }
  32. }
  33.  
  34. if($objEntity->getNumberOfPhoneNumbers()) {
  35. // Posiada numery telefonu
  36. print "<h2>Numery telefonu</h2>";
  37. for($x=0; $x < $objEntity->getNumberOfPhoneNumbers(); $x++) {
  38. println($objEntity->phonenumbers($x)->__toString());
  39. }
  40. }
  41.  
  42. print "<hr>\n";
  43. }
  44. ?>

Luneth
Błąd masz w funkcji die(), wywal ten znak:
Cytat
`

Btw, imho genialny błąd winksmiley.jpg
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.