Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Problemy z napisaniem klasy, tworzę klasę do obsługi mysql
-Gość_KRCreater-
post 11.09.2005, 19:53:41
Post #1





Goście







Wtiam!
Właśnie ucze się programowania obiektowego. Strukturalne mi się znudziło tongue.gif , a po za tym jestem głudny wiedzy. Moje pytanie brzmi hehe dlaczego mi to niedziała tongue.gif Tzn. zmienne z pliku config.php nie pojawiają się w klasie :/

klasa sql
  1. <?php
  2. include("config.php");
  3.  
  4.  
  5.  class sql{
  6.  
  7.  
  8.  // DANE PODTRZEBNE DO POLACZENIA
  9.  var $sql_host;
  10.  var $sql_admin;
  11.  var $sql_pass;
  12.  var $sql_db;
  13.  
  14.  
  15.  
  16.  // POLACZENIE Z BAZA DANYCH MYSQL
  17.  function sql(){
  18.  
  19. public $sql_host;
  20.  
  21. $sql = mysql_connect($sql_host,$sql_admin,$sql_pass) or die("BRAK POŁACZENIA Z BAZA DANYCH :(");
  22. mysql_select_db($sql_db);
  23.  
  24. echo("polaczenie<br>");
  25. echo($sql_host."<br>");
  26. echo($sql_pass."<br>");
  27. echo($sql_admin."<br>");
  28. echo($sql_db."<br>");
  29.  
  30.  }
  31.  
  32.  }
  33.  
  34. $connect = new sql;
  35. ?>


plik config.php
  1. <?php
  2. $sql_host  = "localhost";
  3. $sql_admin = "krasnal";
  4. $sql_pass  = "krasnal";
  5. $sql_db  = "krasnal";
  6. ?>


ps. Proszę na mnie nie krzyczeć tongue.gif
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 11)
dr_bonzo
post 11.09.2005, 19:56:53
Post #2





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

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


Bo sa poza scopem (zakresem widzialnosci zmiennych) klasy.
Przekaz je konstruktorowi, np
  1. <?php
  2. $connect = new sql( $user, $haslo, itd );
  3. ?>


PS. Nie uzywaj 'global'

EDIT: usunalem wykrzykniki biggrin.gif

Ten post edytował dr_bonzo 11.09.2005, 19:57:55


--------------------
Nie lubię jednorożców.
Go to the top of the page
+Quote Post
-Gość_KRCreater-
post 11.09.2005, 20:03:45
Post #3





Goście







Dzięki za szybką odpowiedź, ale ja albo jestem głupie (hehe to wyjście jest najprawdopodobniejsze :]) albo klasa nadal nie widzi zmiennych z pliku config.php wewnątrz siebie.

Zmieniłem:
  1. <?php
  2.  
  3. $connect = new sql;
  4.  
  5. ?>


Na:
  1. <?php
  2.  
  3. $connect = new sql($sql_host,$sql_admin,$sql_pass,$sql_db)
  4.  
  5. ?>
Go to the top of the page
+Quote Post
-Gość_KRCreater-
post 11.09.2005, 20:12:04
Post #4





Goście







Kurczę no nie widzi tych zmiennych tylko global mi działa :/ A napisałeś, że mam globala nie używać (aha mam php 4).

  1. <?php
  2. include("config.php");
  3.  
  4.  
  5. class sql{
  6.  
  7.  
  8.  // DANE PODTRZEBNE DO POLACZENIA
  9.  var $sql_host;
  10.  var $sql_admin;
  11.  var $sql_pass;
  12.  var $sql_db;
  13.  
  14.  //POKAZ ZMIENNE Z PLIKU CONFIG
  15.  function pokaz_config(){
  16.  echo $this->sql_host."<BR>!";
  17.  echo $this->sql_admin."<BR>!";
  18.  echo $this->sql_pass."<BR>!";
  19.  echo $this->sql_db."<BR>!<BR><BR><BR><BR>";
  20.  
  21.  }
  22.  
  23.  
  24.  
  25.  // POLACZENIE Z BAZA DANYCH MYSQL
  26.  function sql(){
  27.  
  28. // global $sql_host,$sql_admin,$sql_pass,$sql_db;
  29.  
  30. $sql = mysql_connect($sql_host,$sql_admin,$sql_pass) or die("BRAK POŁACZENIA Z BAZA DANYCH :(");
  31. mysql_select_db($sql_db);
  32.  
  33.  
  34. echo($sql_host."<br>");
  35. echo($sql_pass."<br>");
  36. echo($sql_admin."<br>");
  37. echo($sql_db."<br>");
  38. echo("polaczenie<br>");
  39.  
  40.  }
  41.  
  42. }
  43.  
  44. $connect = new sql( $sql_host, $sql_admin, $sql_pass, $sql_db );
  45. $connect->pokaz_config();
  46. ?>
Go to the top of the page
+Quote Post
czachor
post 11.09.2005, 20:15:17
Post #5





Grupa: Zarejestrowani
Postów: 897
Pomógł: 40
Dołączył: 16.12.2003
Skąd: Warszawa

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


musisz gdzieś te zmienne odbierać smile.gif
  1. <?php
  2.  
  3.  // POLACZENIE Z BAZA DANYCH MYSQL
  4.  function sql($sql_host, $sql_admin, $sql_pass, $sql_db){
  5.  
  6. ?>


--------------------
how many SEO experts does it take to change a light bulb,lightbulb,light,bulb,lamp,lighting,switch,sex,xxx
5-Reasons-why-you-should-NEVER-fix-a-computer-for-free
Go to the top of the page
+Quote Post
KRC.Creater
post 11.09.2005, 20:44:42
Post #6





Grupa: Zarejestrowani
Postów: 10
Pomógł: 0
Dołączył: 11.09.2005

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


A no tak biggrin.gif hehe
Wielkie dzięki za pomoc, napewno jeszcze się odezwe tutaj :]

Mam kolejny problem tongue.gif Dopisałem troche linijek. Chciałem wyciągnąć za pomocą obiektu connect dane z tabeli jakiejść z mysql'a:

  1. <?php
  2. include("config.php");
  3.  
  4.  
  5. class sql{
  6.  
  7.  
  8.      // DANE PODTRZEBNE DO POLACZENIA
  9.      var $sql_host;
  10.      var $sql_admin;
  11.      var $sql_pass;
  12.      var $sql_db;
  13.      var $n_tabeli;
  14.      var $n_order;
  15.      var $n_where_pole;
  16.      var $n_where_zmienna;
  17.  
  18.  
  19.      // POLACZENIE Z BAZA DANYCH MYSQL
  20.      function sql($sql_host,$sql_admin,$sql_pass,$sql_db){
  21.  
  22.       $sql = mysql_connect($sql_host,$sql_admin,$sql_pass) or die("BRAK POŁACZENIA Z BAZA DANYCH :(");
  23.       mysql_select_db($sql_db);
  24.      }
  25.  
  26.      function tabela(){
  27.  
  28.  
  29.  
  30.        if($this->n_tabeli && $this->n_order && $this->n_where_pole && $this->n_where_zmienna){
  31.         $w = mysql_query("SELECT * FROM '".$this->n_tabeli."' ORDER BY '".$this->n_order."' WHERE '".$this->n_where_pole."'='".$this->n_where_zmienna."' ");
  32.  
  33.          echo $this->n_tabeli."<BR>";
  34.          echo $this->n_where_pole."<BR>";
  35.          echo $this->n_where_zmienna."<BR>";
  36.          echo $this->n_order."<BR>";
  37.        } else {
  38.         $w = mysql_query("SELECT * FROM '".$this->n_tabeli."' ORDER BY '".$this->n_order."'");
  39.        }
  40.  
  41.         $t = mysql_fetch_array($w);
  42.  
  43.      }
  44.  
  45. }
  46.  
  47. $connect = new sql( $sql_host, $sql_admin, $sql_pass, $sql_db );
  48.  
  49. $connect->n_tabeli = 'news';
  50. $connect->n_order = 'DESC';
  51. $connect->n_where_pole= 'id';
  52. $connect->n_where_zmienna= '3';
  53. echo $connect->tabela();
  54.  
  55. ?>


Ale w 41 linijce wywala mi błąd

linijka 41
  1. <?php
  2.  
  3.  $t = mysql_fetch_array($w);
  4.  
  5. ?>


Ten post edytował KRC.Creater 11.09.2005, 20:45:49
Go to the top of the page
+Quote Post
czachor
post 11.09.2005, 21:05:41
Post #7





Grupa: Zarejestrowani
Postów: 897
Pomógł: 40
Dołączył: 16.12.2003
Skąd: Warszawa

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


Przjerzyj przykłady w manualu, pokaż, co wyświetla mysql_error()


--------------------
how many SEO experts does it take to change a light bulb,lightbulb,light,bulb,lamp,lighting,switch,sex,xxx
5-Reasons-why-you-should-NEVER-fix-a-computer-for-free
Go to the top of the page
+Quote Post
Ive
post 11.09.2005, 21:25:51
Post #8





Grupa: Zarejestrowani
Postów: 352
Pomógł: 1
Dołączył: 17.07.2003
Skąd: Kazanów

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


ja tam sie na OOP wogole nie znam ale na moje oko to tez powinienes wstawic te zmienne jako argumenty do funkcji bo jesli ci nie wstawia danych do zapytania [a na pewno o to chodzi] to wywala ci blad mysql w momencie w ktorym sie do niego odwolujesz... walnij za mysql_query :
  1. <?
  2. mysql_query('zapytanie') or die(mysql_error());
  3. ?>


zeby uniknac bledu ze dane nie przedostaja sie do funkcji musisz je znowu wlepic jako argumenty do funkcji:
  1. <?
  2. $connect->tabela('news', 'desc', 'id', '3');
  3. ?>
Go to the top of the page
+Quote Post
KRC.Creater
post 11.09.2005, 22:56:33
Post #9





Grupa: Zarejestrowani
Postów: 10
Pomógł: 0
Dołączył: 11.09.2005

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


Ok doszedłem do tego już biggrin.gif
Klasa mi działa, ale czy wszystko robie poprawnie ?

  1. <?php
  2. include("config.php");
  3.  
  4.  
  5. class sql{
  6.  
  7.  
  8.  // DANE PODTRZEBNE DO POLACZENIA
  9.  var $sql_host;
  10.  var $sql_admin;
  11.  var $sql_pass;
  12.  var $sql_db;
  13.  var $n_tabeli;
  14.  var $n_order_by;
  15.  var $n_order_jak;
  16.  var $n_where_pole;
  17.  var $n_where_zmienna;
  18.  
  19.  
  20.  // POLACZENIE Z BAZA DANYCH MYSQL
  21.  function sql($sql_host,$sql_admin,$sql_pass,$sql_db){
  22.  
  23. $sql = mysql_connect($sql_host,$sql_admin,$sql_pass) or die("BRAK POŁACZENIA Z BAZA DANYCH :(");
  24. mysql_select_db($sql_db) or die("BRAK POŁACZENIA Z BAZA DANYCH :(");
  25.  }
  26.  
  27.  function tabela(){
  28.  
  29.  
  30.  
  31.  if($this->n_tabeli && $this->n_order && $this->n_where_pole && $this->n_where_zmienna){
  32. $w = mysql_query("SELECT * FROM ".$this->n_tabeli." WHERE ".$this->n_where_pole."='".$this->n_where_zmienna."' ORDER BY ".$this->n_order_by." ".$this->n_order_jak) or die(mysql_error());
  33.  
  34.  echo $this->n_tabeli."<BR>";
  35.  echo $this->n_where_pole."<BR>";
  36.  echo $this->n_where_zmienna."<BR>";
  37.  echo $this->n_order."<BR>";
  38.  } else {
  39. $w = mysql_query("SELECT * FROM ".$this->n_tabeli." ORDER BY ".$this->n_order_by." ".$this->n_order_jak) or die(mysql_error());
  40. echo("TO!");
  41.  }
  42.  
  43. $t = mysql_fetch_array($w) or die(mysql_error());
  44. return $t;
  45.  
  46.  }
  47.  
  48. }
  49.  
  50. $connect = new sql( $sql_host, $sql_admin, $sql_pass, $sql_db );
  51.  
  52. $connect->n_tabeli = "news";
  53. $connect->n_order_by = "id";
  54. $connect->n_order_jak = "DESC";
  55. $connect->n_where_pole= "id";
  56. $connect->n_where_zmienna= "3";
  57.  
  58. $t = $connect->tabela();
  59. echo $t['temat'];
  60.  
  61. ?>
Go to the top of the page
+Quote Post
eS...
post 12.09.2005, 11:42:24
Post #10





Grupa: Zarejestrowani
Postów: 367
Pomógł: 2
Dołączył: 4.03.2003
Skąd: C:/Windows/Temp

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


pewnie i dobrze jest, pokaże ci inne rozwiązanie, gdzie troch eprzejzysciej jest
  1. <?
  2. class sql {
  3.   var $dbhost = 'localhost';
  4.   var $dbuser = 'root';
  5.   var $dbpass = '';
  6.   var $dbname = '';
  7.  
  8.   var $result;
  9.   var $row;
  10.  
  11.   function sql_connect()
  12.   {
  13.      mysql_connect($this -> dbhost, $this -> dbuser, $this -> dbpass,$this -> dbname) or die('Nie moge nawiazac polaczenia z baza! Blad: '.mysql_error());
  14.      mysql_select_db($this -> dbname) or die('Nie znalazlem bazy danych! Blad: '.mysql_error());
  15.   }
  16.  
  17.   function sql_query($query)
  18.   {
  19.      if($this -> result = mysql_query($query)){
  20.         return $this -> result;
  21.      }
  22.       if(mysql_errno()){
  23.          die('Błąd MySQLa: '.mysql_error());
  24.     }
  25.  
  26.   }
  27.  
  28.     function sql_fetch_array(){
  29.         if($this->row = mysql_fetch_array($this->result)){
  30.           return $this->row;
  31.         }
  32.     }
  33. }
  34. $sql = new sql;
  35. $sql -> sql_connect();
  36.  
  37. //pytanie do bazy
  38. $a=$sql->sql_query('zapytanie');
  39. //wyniki
  40. $sql->sql_fetch_array($a);
  41.  
  42. ?>  

pozdrawiam


--------------------
Go to the top of the page
+Quote Post
KRC.Creater
post 12.09.2005, 12:14:52
Post #11





Grupa: Zarejestrowani
Postów: 10
Pomógł: 0
Dołączył: 11.09.2005

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


dzięki za tą class'e eS... rzeczywiście jest lepiej napisana, prościej :]
Postaram się ją rozwinąć w tym kierunku, pozdrawiam.
ps. widze, że też się uczysz OOP smile.gif
Go to the top of the page
+Quote Post
eS...
post 12.09.2005, 14:20:25
Post #12





Grupa: Zarejestrowani
Postów: 367
Pomógł: 2
Dołączył: 4.03.2003
Skąd: C:/Windows/Temp

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


ucze sie, raczej mam przerwe teraz smile.gif ale powoli bieram sie na napisanie jakiejs nowej kasy
jak by co JID : eska@chrome.pl


--------------------
Go to the top of the page
+Quote Post

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 Wersja Lo-Fi Aktualny czas: 18.07.2025 - 04:39