Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Problem z klasą do mysql
Kas
post
Post #1





Grupa: Zarejestrowani
Postów: 425
Pomógł: 0
Dołączył: 28.05.2005
Skąd: Warszawa

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


Ostatnio mało bywałem na forum, ponieważ min. tworzyłem [usunąłem linka] alek.
Ale nie w tym problem. biggrin.gif
Stworzyłem klasę do obsługi bazy danych mysql.

  1. <?php
  2. class sql {
  3. private $link;
  4. private $result;
  5. public $rows;
  6.  
  7. public function __construct {
  8.  
  9. $this -> $sql_host = 'localhost'; // Adres serwera bazy danych
  10. $this -> $sql_name = 'forum'; // Nazwa bazy danych
  11. $this -> $sql_user = 'root'; // Nazwa usera
  12. $this -> $sql_password = ''; // Hasło
  13. $this -> $sql_port = '3306'; // Port
  14.  
  15. $this -> link = mysql_connect($this -> $sql_host, $this -> $sql_user, $this -> $sql_password);
  16. if (!is_resource($this -> link) {
  17. die('Brak połączenia z bazą danych.' . mysql_error());
  18. }
  19. }
  20.  
  21. public function __destruct () {
  22. if(is_resource($this -> link)) {
  23. mysql_close($this -> link);
  24. }
  25. }
  26.  
  27. public function query($query) {
  28.  $this -> result = mysql_query($query, $this -> link);
  29.  }
  30.  
  31. public function fetch_row() {
  32.  if($this -> rows = mysql_fetch_row($this -> result)){
  33. return 1;
  34. }
  35. }
  36. }
  37. ?>


I nie działa. sad.gif
Cały kod wygląda tak.

  1. <?php
  2.  
  3. class sql {
  4. private $link;
  5. private $result;
  6. public $rows;
  7.  
  8. public function __construct {
  9.  
  10. $this -> $sql_host = 'localhost'; // Adres serwera bazy danych
  11. $this -> $sql_name = 'forum'; // Nazwa bazy danych
  12. $this -> $sql_user = 'root'; // Nazwa usera
  13. $this -> $sql_password = ''; // Hasło
  14. $this -> $sql_port = '3306'; // Port
  15.  
  16. $this -> link = mysql_connect($this -> $sql_host, $this -> $sql_user, $this -> $sql_password);
  17. if (!is_resource($this -> link) {
  18. die('Brak połączenia z bazą danych.' . mysql_error());
  19. }
  20. }
  21.  
  22. public function __destruct () {
  23. if(is_resource($this -> link)) {
  24. mysql_close($this -> link);
  25. }
  26. }
  27.  
  28. public function query($query) {
  29.  $this -> result = mysql_query($query, $this -> link);
  30.  }
  31.  
  32. public function fetch_row() {
  33.  if($this -> rows = mysql_fetch_row($this -> result)){
  34. return 1;
  35. }
  36. }
  37. }
  38.  
  39. $sql = new sql;
  40. $sql -> $query('SELECT username FROM users');
  41.  
  42. while($sql -> fetch_row()){
  43. echo '<li>'.$sql -> rows[0].'</li>';
  44. }
  45. echo '</ul>';
  46.  
  47. ?>


Co jest źle?


--------------------
nospor, jestem z Wami.
Alpha IT
Go to the top of the page
+Quote Post

Posty w temacie


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 Aktualny czas: 22.08.2025 - 05:40