Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Klasa - łączenie z bazą i pobieranie danych, problem
Reiven
post
Post #1





Grupa: Zarejestrowani
Postów: 100
Pomógł: 1
Dołączył: 17.03.2006

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


Witam,
mam pewną klase, która łaczy mi się z bazą danych i ma pobierać dane z niej.

klasy.php
  1. <?php
  2.  
  3. /**
  4. * @author Weogrim
  5. * @copyright 2009
  6. * Klasa do obsługi MySQL
  7. */
  8.  
  9. class db {
  10. private $db_host = 'localhost';
  11. private $db_name = 'klasy';
  12. private $db_user = 'root';
  13. private $db_pass = '';
  14. public $errors = 0;
  15. public $queries = 0;
  16. public $result;
  17. private $connection;
  18. public $time = 0;
  19.  
  20. public FUNCTION __construct() {
  21.  
  22. $this->connection = @mysql_connect($this->db_host, $this->db_user, $this->db_pass)
  23. OR die('Brak połączenia z serwerem MySQL. <br>Błąd: '.mysql_error());
  24. IF (!$this->connection) {
  25. $this->errors++;
  26. }else{
  27. $db = @mysql_select_db($this->db_name, $this->connection)
  28. OR die('Nie mogę połączyć się z bazą danych. <br>Błąd: '.mysql_error());
  29. }
  30. }
  31.  
  32. public FUNCTION close() {
  33. mysql_close($this->connection);
  34. }
  35.  
  36. public FUNCTION query($query) {
  37. $start = microtime(true);
  38. $this->result = @mysql_query($query);
  39. IF(!$this->result){
  40. $this->errors++;
  41. RETURN false;
  42. }else{
  43. $this->queries++;
  44. $this->time += microtime(true) - $start;
  45. RETURN true;
  46. }
  47. }
  48.  
  49. public FUNCTION fetch_array($result) {
  50.  
  51. $array = mysql_fetch_array(mysql_query($result));
  52. IF(!$array) {
  53. RETURN false;
  54. $this->errors++;
  55. }else{
  56. RETURN $array;
  57. $this->queries++;
  58.  
  59.  
  60.  
  61. }
  62. }
  63.  
  64. public FUNCTION rows($result) {
  65. $rows = mysql_num_rows(mysql_query($result));
  66. IF(!$rows) {
  67. RETURN false;
  68. $this->errors++;
  69. }else{
  70. $this->queries++;
  71. RETURN $rows;
  72. }
  73. }
  74.  
  75. }
  76.  
  77. ?>


index.php
  1. <?php
  2. require_once "klasy.php";
  3. $db = new db();
  4. #$gryonline = $db->fetch_array("SELECT * FROM gryonline");
  5.  
  6. $result = ("SELECT * FROM gryonline");
  7.  
  8. while($row = $db->fetch_array($result)) {
  9. echo $row['nazwa_gry'];
  10.  
  11. }
  12. ?>


z pobraniem jednego wpisu nie mam problemu, problem zaczyna się gdy chcę pobrać np wszystkie wpisy,,,

Ten post edytował Reiven 19.11.2009, 21:31:00
Go to the top of the page
+Quote Post

Posty w temacie


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: 28.09.2025 - 03:51