Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Łączenie z bazą za pomocą klasy, Problem z tworzeniem obiektu
mysql_php
post
Post #1





Grupa: Zarejestrowani
Postów: 36
Pomógł: 0
Dołączył: 1.08.2006

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


Witam, napisałem taki skrypt do łączenia się z bazą postgrees, prosty ponieważ dopiero się uczę:

  1. <?php
  2. class db {
  3. var $dbname;
  4. var $connection;
  5. var $result;
  6.  
  7.  
  8.  
  9.  
  10. function db($user, $dbname, $host, $password, $port){
  11. $this-> connection = @pg_connect($user,$dbname,$host,$password,$port);
  12. }
  13.  
  14. function query($query){
  15. $this-> query = $query;
  16. $this-> result = pg_exec($this->connection, $this->query);
  17. }
  18.  
  19. function close(){
  20. pg_close($this->connection);
  21. }
  22. }
  23. ?>


Niestety nie wiem, jak potem korzystać z tej klasy sad.gif
Mam plik "zawartosc.php" W którym includuję plik klasy, próbuję nawiązać połączenie z bazą
za pomocą klasy i dodać jakiś rekord, wiem, że samo łączenie powinno wyglądać tak:

  1. <?php
  2. $db = new db('aaaaa', 'bbbbb', 'gggg.gg.gg', 'tajne', '54321');
  3. ?>


niestety nie wiem jak się "dobrac" do $query, $result oraz pg_close();

Czy mógłby mi ktoś pomóc i napisac jak będzie wyglądało takie kompletne połączenie?

Ten post edytował mysql_php 14.01.2007, 09:10:17
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 12)
Cysiaczek
post
Post #2





Grupa: Moderatorzy
Postów: 4 465
Pomógł: 137
Dołączył: 26.03.2004
Skąd: Gorzów Wlkp.




  1. <?php
  2. $db = new db('aaaaa', 'bbbbb', 'gggg.gg.gg', 'tajne', '54321');
  3. $db->query($query);
  4. $db->query($kolejne_query);
  5. $db->close();
  6. ?>


Pozdrawiam.


--------------------
To think for yourself you must question authority and
learn how to put yourself in a state of vulnerable, open-mindedness;
chaotic, confused, vulnerability, to inform yourself.
Think for yourself. Question authority.
Go to the top of the page
+Quote Post
mysql_php
post
Post #3





Grupa: Zarejestrowani
Postów: 36
Pomógł: 0
Dołączył: 1.08.2006

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


Bardzo dziękuję,
Teraz robię tak jak poniżej ale baza zwraca mi błąd sad.gif

Robię tak:
  1. <?php
  2. if($_POST['skrypt'] == 'dodajf'){
  3. $db = new db('aaaaa', 'bbbbb', 'gggg.gg.gg', 'tajne', '54321');
  4.  
  5. $db -> $query("insert into w_filmy (nazwa_oryg, nazwa_pl, gatunek, czas, nosnik, rok_prod, kraj, od_lat, premie
    ra, status) values('"
    .$_POST['nazwa_oryg']."','".$_POST['nazwa_pl']."','".$_POST['gatunek']."','".$_POST['czas']."','".$_POST['nosnik']."','".$_POST['rok_prod']."','".$_POST['kraj']."','".$_POST['od_lat']."','".$_POST['premiera']."','0'");
  6.  
  7. $db -> $result;// = pg_exec($query);
  8.  
  9. $db->close();
  10.  
  11. };
  12. ?>


Niestety pojawia się komunikat:

  1. <?php
  2. Fatal error: Method name must be a string in /home/informatyka/public_html/zawartosc.php on line 15
  3. ?>


linia 15 to linia gdzie jest instert do bazy...
Go to the top of the page
+Quote Post
Cysiaczek
post
Post #4





Grupa: Moderatorzy
Postów: 4 465
Pomógł: 137
Dołączył: 26.03.2004
Skąd: Gorzów Wlkp.




Przypatrz się uważnie i znajdź błąd
  1. <?php
  2. $db -> $result;
  3. ?>


Doczytaj jak się stosuje nazwy metod i właściwości klasy w kontekście obiektowym.

Ten post edytował Cysiaczek 14.01.2007, 09:51:49


--------------------
To think for yourself you must question authority and
learn how to put yourself in a state of vulnerable, open-mindedness;
chaotic, confused, vulnerability, to inform yourself.
Think for yourself. Question authority.
Go to the top of the page
+Quote Post
mysql_php
post
Post #5





Grupa: Zarejestrowani
Postów: 36
Pomógł: 0
Dołączył: 1.08.2006

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


Faktycznie, błędem było używanie znaków dolara po ->
dzięki za wymuszenie znalezienia błędu smile.gif

Wydaje mi się, że już wszystrko poprawiłem a tutaj kolejny błąd sad.gif

  1. <?php
  2. Warning: pg_exec(): supplied argument is not a valid PostgreSQL link resource in /home/informatyka/public_html/klasa.php on line 17
  3.  
  4. Warning: pg_exec(): supplied argument is not a valid PostgreSQL link resource in /home/informatyka/public_html/klasa.php on line 21
  5. ?>


Czyli teraz okazuje się, że coś nie tak zrobiłem samą klasę sad.gif
PRoszę o pomoc bo już nei wiem gdzie może byc błąd
Go to the top of the page
+Quote Post
Cienki1980
post
Post #6





Grupa: Przyjaciele php.pl
Postów: 1 590
Pomógł: 40
Dołączył: 11.01.2007
Skąd: Centrum

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


A spróbuj zrobić tak, że db zwraca Ci wartość ( ID połączenia do bazy ) i potem podawaj ją do metody query.


--------------------
404
Go to the top of the page
+Quote Post
ikioloak
post
Post #7





Grupa: Zarejestrowani
Postów: 416
Pomógł: 0
Dołączył: 8.01.2004

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


Usun @ sprzed pg_connect(), oraz dla celow testowych zmien funkcje query na:
  1. <?php
  2. function query($query){
  3. $this->query = $query;
  4. $this->result = pg_exec($this->connection, $this->query);
  5. if (!$this->result) {
  6. die('blad' . pg_result_error($this->result));
  7. }
  8. }
  9. ?>


Ten post edytował ikioloak 14.01.2007, 10:19:01
Go to the top of the page
+Quote Post
mysql_php
post
Post #8





Grupa: Zarejestrowani
Postów: 36
Pomógł: 0
Dołączył: 1.08.2006

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


sorry ale nie zabardzo rozumiem...
jak to podawać w query db?
Go to the top of the page
+Quote Post
Cienki1980
post
Post #9





Grupa: Przyjaciele php.pl
Postów: 1 590
Pomógł: 40
Dołączył: 11.01.2007
Skąd: Centrum

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


  1. <?php
  2. class db 
  3. {
  4. var $dbname;
  5. var $connection;
  6. var $result;
  7.  
  8. function db()
  9. {
  10.  return true;
  11. }
  12.  
  13. function db_c($user, $dbname, $host, $password, $port)
  14. {
  15.  return @pg_connect($user,$dbname,$host,$password,$port);
  16. }
  17. function query($link,$query)
  18. {
  19.  $this-> query = $query;  
  20.  $this->link=$link;
  21.  $this-> result = pg_exec($this->link, $this->query);
  22. }
  23. function close()
  24. {  
  25. pg_close($this->connection);  
  26.  }
  27. }
  28. ?>


potem w pliku:
  1. <?php
  2. $db= new db();
  3. $link=$db->db_c('aaaaa', 'bbbbb', 'gggg.gg.gg', 'tajne', '54321');
  4. $query="select * from table_name";
  5. $db->query($link,$query);
  6. ?>


Pisałem na szybko i jest wcześnie rano więc mogą być niedoskonałości ... chodzi mi o sam koncept.


PS. piszesz takie coś
  1. <?php
  2. $db->result()
  3. ?>

a w pokazanym kodzie klasy nie masz metody result();


--------------------
404
Go to the top of the page
+Quote Post
mysql_php
post
Post #10





Grupa: Zarejestrowani
Postów: 36
Pomógł: 0
Dołączył: 1.08.2006

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


Zmieniłem klasę wedug zalecen:
  1. <?php
  2. class db {
  3. var $dbname;
  4. var $connection;
  5. var $result;
  6.  
  7.  
  8.  
  9.  
  10. function db($user, $dbname, $host, $password, $port){
  11. $this-> connection = @pg_connect($user,$dbname,$host,$password,$port);
  12. }
  13.  
  14. function query($query){
  15. $this-> query = $query;
  16. $this-> result = pg_exec($this->connection, $this->query);
  17.  
  18. if (!$this->result) {
  19. die('blad' . pg_result_error($this->result));
  20. }
  21. }
  22.  
  23. function close(){
  24. pg_close($this->connection);
  25. }
  26. }
  27. ?>


Pozostawiłem jedynie @ przed pg_connect bo po jej usunięciu pojkawiało się jescze więcej błędów smile.gif

Teraz mam komunikat taki:
  1. <?php
  2. Warning: pg_exec(): supplied argument is not a valid PostgreSQL link resource in /home/informatyka/public_html/klasa.php on line 17
  3. blad
  4. ?>


Linia 17 w mojej klasie to:
  1. <?php
  2. $this-> result = pg_exec($this->connection, $this->query);
  3. ?>
Go to the top of the page
+Quote Post
Cienki1980
post
Post #11





Grupa: Przyjaciele php.pl
Postów: 1 590
Pomógł: 40
Dołączył: 11.01.2007
Skąd: Centrum

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


No skoro po usunięciu @ przed pg_connect pokazuje się więcej błędów to znaczy, że nie jest poprawne przeprowadzone połączenie do bazy.

I to samo wynika z błędu cytowanego przez Ciebie.
Pokaż jakie błędy pokazują się jak usuniesz @ sprzed pg_connect.


--------------------
404
Go to the top of the page
+Quote Post
mysql_php
post
Post #12





Grupa: Zarejestrowani
Postów: 36
Pomógł: 0
Dołączył: 1.08.2006

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


Ok, to podsumowując:

klasy uzywam takiej:
  1. <?php
  2.  
  3. class db {
  4. var $dbname;
  5. var $connection;
  6. var $result;
  7.  
  8. function db($user, $dbname, $host, $password, $port){
  9.  $this-> connection = pg_connect($user,$dbname,$host,$password,$port);
  10.  }
  11.  
  12. function query($query){
  13. $this-> query = $query;
  14. $this-> result = pg_exec($this->connection, $this->query);
  15.  
  16. if (!$this->result) {
  17. die('blad' . pg_result_error($this->result));
  18. }
  19. }
  20.  
  21. function close(){
  22. pg_close($this->connection);
  23. }
  24.  
  25. }
  26. ?>


W pliku używam jej tak:

  1. <?php
  2. include('klasa.php');
  3.  
  4. if($_POST['skrypt'] == 'dodajf'){
  5.  
  6. $db = new db('romek', 'romek', 'xxxx.xx.xxx', 'xxxxxxxxxxx', '54321');
  7.  
  8. $db -> query("insert into w_filmy (nazwa_oryg, nazwa_pl, gatunek, czas, nosnik, rok_prod, kraj, od_lat, premie
    ra, status) values('"
    .$_POST['nazwa_oryg']."','".$_POST['nazwa_pl']."','".$_POST['gatunek']."','".$_POST['czas']."','".$_POST['nosnik']."','".$_POST['rok_prod']."','".$_POST['kraj']."','".$_POST['od_lat']."','".$_POST['premiera']."','0'");
  9.  
  10. $db -> result;// = pg_exec($query);
  11. $db-> close();
  12. };
  13. ?>


Po próbie dodanie jakieś rekordu, mam komunikat:
  1. <?php
  2. Warning: pg_connect() [function.pg-connect]: Unable to connect to PostgreSQL server: could not translate host name "romek" to address: Name or service not known in /home/informatyka/public_html/klasa.php on line 9
  3.  
  4. Warning: pg_exec(): supplied argument is not a valid PostgreSQL link resource in /home/informatyka/public_html/klasa.php on line 14
  5. blad
  6. ?>


Już nie wiem co jest źle sad.gif
Go to the top of the page
+Quote Post
Cienki1980
post
Post #13





Grupa: Przyjaciele php.pl
Postów: 1 590
Pomógł: 40
Dołączył: 11.01.2007
Skąd: Centrum

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


  1. <?php
  2. Warning: pg_connect() [function.pg-connect]: Unable to connect to PostgreSQL server: could not translate host name "romek" to address: Name or service not known in /home/informatyka/public_html/klasa.php on line 9
  3. ?>


To Ci nic nie mówi questionmark.gif?

Nie możesz połączyć się z serwerem postgre ...
Skoro nie ma połączenia, nie ma możliwości wykonania poprawnie zapytania.


--------------------
404
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 Aktualny czas: 22.08.2025 - 04:46