Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [php] błąd w klasie
Lejto
post
Post #1





Grupa: Zarejestrowani
Postów: 1 385
Pomógł: 48
Dołączył: 23.05.2007

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


Mam taki fragment index.php
  1. include("config.php");
  2. include("libs/db.class.php");
  3. $db = new DB();
  4. include("libs/core.class.php");
  5. $c = new core($db);
  6. include("function.inc.php");
  7. $f = new functions();

wszystko chodzi
wchodzę na stronę gdzie rejestruje usera
połączenie z bazą jest wywołuje funkcje sprawdzającą
  1. global $db;
  2. global $page;
  3. global $f;
  4.  
  5. include("libs/user.class.php");
  6. $u = new User($db);
  7.  
  8. $user = $u->getByUsername($_POST['username']);

połączenie z bazą w tym pliku jest ale nie ma już jego w klasie user.class.php jak juz wcześniej wymieniłem plik code.class.php (tam wszystko działa)
user.class.php
  1. class User
  2. {
  3. public $uid;
  4. public $fields = array();
  5.  
  6. public function __construct($sql)
  7. {
  8. $this->sql = $sql;
  9. $this->uid = null;
  10. $this->fields = array('username' => '',
  11. 'password' => '',
  12. 'emailAddr' => '',
  13. 'isActive' => false);
  14. }
  15. public static function getByUsername($username)
  16. {
  17. echo 'aa';
  18. $db = $this->sql;
  19. echo 'aa';
  20. $sql = $db->query('select * from users where username = $username');
  21.  
  22. }

db.class.php
  1. class DB{
  2.  
  3. public $_lacz;
  4.  
  5. public function __construct()
  6. {
  7. $this->_lacz = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  8. if(mysqli_connect_errno() != 0)
  9. {
  10. echo '<p>Wystąpił błąd połączenia: ' . mysqli_connect_error(). ', przepraszamy.';
  11. }
  12. }

dlaczego nie mam połączenie w klasie user?
reszta klas działa normalnie
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Lejto
post
Post #2





Grupa: Zarejestrowani
Postów: 1 385
Pomógł: 48
Dołączył: 23.05.2007

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


nie za bardzo cię rozumiem, poprawiłem klase, rejestruje ale nie sprawdza czy nazwa użytkownika się powtarza ;/
  1. <?
  2. class User extends functions
  3. {
  4. public $uid;
  5. public $fields = array();
  6. protected $sql;
  7.  
  8. public function __construct($sql)
  9. {
  10. $this->sql = $sql;
  11. $this->uid = null;
  12. $this->fields = array('username' => '',
  13. 'password' => '',
  14. 'emailAddr' => '',
  15. 'isActive' => false);
  16. }
  17. public function __get($field)
  18. {
  19. if($field == 'userId')
  20. {
  21. return $this->uid;
  22. }
  23. else
  24. {
  25. return $this->fields[$field];
  26. }
  27. }
  28. public function __set($field, $value)
  29. {
  30. if(array_key_exists($field, $this->fields))
  31. {
  32. $this->fields[$field] = $value;
  33. }
  34. }
  35. public function validateUserName($username)
  36. {
  37. if(strlen($username)<3)
  38. {
  39. echo 'Nazwa użytkownika musi zawierać co najmniej 3 znaki';
  40. }
  41. else
  42. {
  43. return true;
  44. }
  45. if(strlen($username)>20)
  46. {
  47. echo 'Za długa nazwa użytkownika max 20 znaków';
  48. }
  49. else
  50. {
  51. return true;
  52. }
  53. }
  54. public static function validateEmailAddr($email)
  55. {
  56. return filter_var($email, FILTER_VALIDATE_EMAIL);
  57. }
  58. public function getById($uid)
  59. {
  60. $db = $this->sql;
  61. $u = new User($db);
  62. $sql = $db->query('select username, password, email_addr, is_active from users where userid = $uid');
  63.  
  64. if($sql->num_rows)
  65. {
  66. $row = $sql->fetch_array();
  67. $u->username = $row['username'];
  68. $u->password = $row['password'];
  69. $u->emailAddr = $row['email_addr'];
  70. $u->isActive = $row['is_active'];
  71. $u->uid = $uid;
  72. }
  73. return $u;
  74. }
  75.  
  76. public function getByUsername($username)
  77. {
  78. $db = $this->sql;
  79. $u = new User($db);
  80.  
  81. $sql = $db->query('select user_id, password, email_addr, is_active from USERS where username = $username');
  82.  
  83. if($sql->num_rows)
  84. {
  85. $row = $sql->fetch_array();
  86. echo $row['username'];
  87. echo 'aa';
  88. $u->password = $row['password'];
  89. $u->emailAddr = $row['email_addr'];
  90. $u->isActive = $row['is_active'];
  91. $u->uid = $row['user_id'];
  92. }
  93. return $u;
  94. }
  95.  
  96. public function save()
  97. {
  98.  
  99.  
  100. $db = $this->sql;
  101. if($this->uid)
  102. {
  103. $sql = $db->query('update USERS set username = $this->username, password = $this->password,
  104. email_addr = $this->emailAddr, is_active = $this->isActive where user_id = $this->userId');
  105. }
  106. else
  107. {
  108. $sql = $db->query('insert into users (username, password, email_addr, is_active)
  109. values ("'.$this->username.'", "'.$this->password.'", "'.$this->emailAddr.'", "'.$this->isActive.'")');
  110. if(!$sql)
  111. {
  112. var_dump($this->password);
  113. }
  114. }
  115. }
  116.  
  117. public function setInactive()
  118. {
  119. $db = $this->sql;
  120. $this->isActive = false;
  121. $this->save();
  122.  
  123. $token = $this->random_text(5);
  124. $sql = $db->query('insert into users_pending (user_id, token) values ($this->uid, $token)');
  125.  
  126. return $token;
  127. }
  128.  
  129. public function setActive($token)
  130. {
  131. $db = $this->sql;
  132.  
  133. $sql = $db->query('select token from users_pending where user_id = $this->uid and token = $token');
  134.  
  135. if(!$sql->num_rows)
  136. {
  137. return false;
  138. }
  139. else
  140. {
  141. $sql = $db->query('delete from users_pending where user_id = $this->uid and token = $this->token');
  142. $this->isAdcive = true;
  143. $this->save();
  144. return true;
  145. }
  146. }
  147. }
  148. ?>
Go to the top of the page
+Quote Post

Posty w temacie
- Lejto   [php] błąd w klasie   4.01.2010, 20:45:02
- - darko   Gdzie masz pole $sql w klasie User ?   4.01.2010, 20:47:10
- - Lejto   jak pole $sql? w core.class mam [PHP] pobier...   4.01.2010, 20:50:48
- - darko   Nie zauważyłem że public static function getByUser...   4.01.2010, 20:53:03
- - skowron-line   [PHP] pobierz, plaintext public [url="htt...   4.01.2010, 20:55:12
- - darko   Jak usuniesz static, to dodaj jeszcze w klasie Use...   4.01.2010, 20:58:05
- - Lejto   usunąłem ale nadal nie chce wykonać zapytania va...   4.01.2010, 21:00:21
- - darko   a dodałeś to pole protected $sql; ?   4.01.2010, 21:01:15
- - Lejto   jakie protected? mam normalną prywatność dla tego...   5.01.2010, 09:08:05
- - darko   Metoda getByUsername nie operuje na polu userId   5.01.2010, 10:48:06
- - Lejto   ale mam to w __get no to jest mam to poprawić?   5.01.2010, 15:34:45
- - darko   Wypełnij pola klasy User analogicznie, jak w metod...   5.01.2010, 15:53:00
- - Lejto   nie za bardzo cię rozumiem, poprawiłem klase, reje...   5.01.2010, 17:18:56
- - Crozin   To może najpierw kilka uwag: #2: Klasa 'User...   5.01.2010, 17:51:34
- - Lejto   dziedziczę jeszcze jeszcze functions bo jest tam f...   5.01.2010, 22:25:27
- - Crozin   Cytatdziedziczę jeszcze jeszcze functions bo jest ...   5.01.2010, 23:40:26
- - Lejto   ok dzięki ale powiedź jeszcze dlaczego nie chce ...   6.01.2010, 18:15:24
- - Crozin   Wyświetl sobie błędy zapytań MySQL...   6.01.2010, 18:32:55
- - Lejto   nie wpadł bym na to problem rozwiązany, błąd był ...   6.01.2010, 20:36:23


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

 



RSS Aktualny czas: 4.10.2025 - 06:26