Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][MySQL]problem z baza, php5
Forum PHP.pl > Forum > Przedszkole
El Nino9
witam, uczę się dopiero programować obiektowo, korzystam z ksiązki php5. zaawansowane programowanie


  1. <?php
  2. require_once('class.Entity.php');
  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. if (isset($hDB)) {
  11. return $hDB;
  12. }
  13.  
  14. $hDB = mysql_connect('localhost' , 'root')
  15. or die ("blad polaczenia z baza danych");
  16. $db = mysql_select_db('cms')
  17. or die ("blad wybierania bazy danych");
  18.  
  19. return $hDB;
  20.  
  21. }
  22.  
  23. public static function getAddressData($addressID) {
  24. $sql = "SELECT * from \"adres\" where \"adres_id\" = $addressID";
  25. $res = mysql_query(DataManager::_getConnection(). $sql);
  26. if (! ($res and mysql_num_rows($res))) {
  27. die ("Blad odczytu danych dla adresu pocztowego $addressID");
  28. }
  29.  
  30. return mysql_fetch_assoc($res);
  31. }
  32.  
  33. public static function getEmailData($emailID) {
  34.  
  35. $sql = "select * from \"email\" where \"email_id\" = $emailID";
  36. $res = mysql_query(DataManager::_getConnection(). $sql);
  37. if (! ($res and mysql_num_rows($res))) {
  38. die ("blad odczytu danych dla adresu email $emailID");
  39. }
  40.  
  41. return mysql_fetch_assoc($res);
  42. }
  43.  
  44. public static function getPhoneNumberData($phoneID) {
  45.  
  46. $sql = "SELECT * from \"telefon\" where \"telefon_id\" = $phoneID";
  47. $res = mysql_query(DataManager::_getConnection(). $sql);
  48. if (! ($res and mysql_num_rows($res))) {
  49. die ("blad odczytu danych dla numeru telefonu $phoneID");
  50. }
  51.  
  52. return mysql_fetch_assoc($res);
  53.  
  54. }
  55.  
  56. public static function getEntityData($entityID) {
  57. $sql = "SELECT * FROM \"jednostka\" where \"jednostka_id\" = $entityID";
  58. $res = mysql_query(DataManager::_getConnection(). $sql);
  59. if (! ($res and mysql_num_rows($res))) {
  60. die("blad odczytu jednostki $entityID");
  61. }
  62.  
  63. return mysql_fetch_assoc($res);
  64. }
  65.  
  66. public static function getAddressObjectsForEntity($entityID) {
  67. $sql = "select \"adres_id\" from \"adres\" where \"jednostka_id\" = $entityID";
  68. $res = mysql_query(DataManager::_getConnection(). $sql);
  69.  
  70. if (!$res) {
  71. die("blad oczytu adresow dla jednostki $entityID");
  72. }
  73.  
  74. if (mysql_num_rows($res)) {
  75. $objs = array();
  76. while($rec = mysql_fetch_assoc($res)) {
  77. $objs[] = new Address($rec['adres_id']);
  78. }
  79. return $objs;
  80. } else {
  81. return array();
  82. }
  83. }
  84.  
  85. public static function getEmailObjectsForEntity($entityID) {
  86. $sql = "SELECT \"email_id\" from \"email\" WHERE \"jednostka_id\" = $entityID";
  87. $res = mysql_query(DataManager::_getConnection(). $sql);
  88. if(!$res) {
  89. die ("blad odczytu adresow email dla jednostki $entityID");
  90. }
  91.  
  92. if (mysql_num_rows($res)) {
  93. $objs = array();
  94. while ($rec = mysql_fetch_assoc($res)) {
  95. $objs[] = new EmailAddress($rec['email_id']);
  96. }
  97. return $objs;
  98. } else {
  99. return array();
  100. }
  101. }
  102. public static function getPhoneNumberObjectsForEntity($entityID) {
  103. $sql = "SELECT \"telefon_id\" from \"telefon\" WHERE \"jednostka_id\" = $entityID";
  104. $res = mysql_query(DataManager::_getConnection(). $sql);
  105.  
  106. if (!$res) {
  107. die ("blad odczytu numerow telefonu dla jednostki $entityID");
  108. }
  109. if (mysql_num_rows($res)) {
  110. $objs = array();
  111. while ($rec = mysql_fetch_assoc($res)) {
  112. $objs[] = new PhoneNumber($rec['telefon_id']);
  113. }
  114. return $objs;
  115. } else {
  116. return array();
  117. }
  118. }
  119. public static function getEmployer($individualID) {
  120. $sql = "SELECT \"organizacja_id\" FROM \"zatrudniony\" WHERE \"osoba_id\" = $individualID ";
  121. $res = mysql_query(DataManager::_getConnection(). $sql);
  122. if (!($res and mysql_num_rows($res))) {
  123. die ("blad odczytu pracodawcy dla osoby $individualID");
  124. }
  125.  
  126. $row = mysql_fetch_assoc($res);
  127.  
  128. if ($row) {
  129. return new Organization($row['organizacja_id']);
  130. } else {
  131. return null;
  132. }
  133. }
  134.  
  135. public static function getEmployees($orgID) {
  136. $sql = "SELECT \"osoba_id\" FROM \"zatrudniony\" WHERE \"organizacja_id\" = $orgID";
  137. $res = mysql_query(DataManager::_getConnection(). $sql);
  138.  
  139. if (! ($res and mysql_num_rows($res))) {
  140. die ("blad odczytu informacji o zatrudnionych dla organizacji $orgID");
  141. }
  142. if (mysql_num_rows($res)) {
  143. $objs = array();
  144. while ($row = mysql_fetch_assoc($res)) {
  145. $objs[] = new Individual($row['osoba_id']);
  146. }
  147. return $objs;
  148. } else {
  149. return array();
  150. }
  151. }
  152.  
  153. public static function getAllEntitiesAsObjects() {
  154. $sql = "SELECT \"jednostka_id\" , \"typ\" from \"jednostka\"";
  155. $res = mysql_query(DataManager::_getConnection(). $sql);
  156.  
  157. if (!$res) {
  158. die ("blad pobierania wszystkich jednostek");
  159. }
  160.  
  161. if (mysql_num_rows($res)) {
  162. $objs = array();
  163. while($row = mysql_fetch_assoc($res)) {
  164. if ($row['typ'] == 'I') {
  165. $objs[] = new Individual($row['jednostka_id']);
  166. } elseif ($row['typ'] == 'O') {
  167. $objs[] = new Organization($row['jednostka_id']);
  168. } else {
  169. die ("nieznany typ jednostki {$row['typ']} !");
  170. }
  171. }
  172. return $objs;
  173. } else {
  174. return array();
  175. }
  176. }
  177. }


przy wywolaniu jakiejkolwiek metody występuje błąd pobierania z bazy, natomiast samo połączenie z baza jest nawiązanie na samym początku oczwyiście z powodzeniem
podejrzewam ze chodzi o to, ze w ksiazce polaczenie bylo nawiązywane tak:
  1. $hDB = pg_connect("host=localhost port=5432 dbname=przykladowa user=php password=php")

natomiast w przypadku mysql nawiazanie w ten sposob o ile wiem jest niemozliwe
jak sobie z tym poradzic?
pozdrawiam

?>
sazian
no tak błąd połączenia z bazą danych mówi bardzo wiele wink.gif
daj mysql_error()

a tak swoją drogą podejrzewam że chodzi o brak hasła
CuteOne
źle:
$res = mysql_query(DataManager::_getConnection(). $sql);

(tak jakby)dobrze:
$res = mysql_query($sql, DataManager::_getConnection());

ps. przed nauką OOP polecam nauczyć się podstaw wink.gif
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.