Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Potrzebuje małej pomocy z klasami
Raven1122
post 29.01.2013, 20:17:34
Post #1





Grupa: Zarejestrowani
Postów: 369
Pomógł: 2
Dołączył: 1.11.2010

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


Witam,

Mam takie klasy:

  1. <?php
  2. require_once("config.php");
  3. class mysql{
  4.  
  5. public function __construct(){
  6. $db = new PDO('mysql:host='.HOST.';dbname='.DB.'', USERNAME, PASSWORD);
  7. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  8. }
  9.  
  10. }
  11.  
  12. class sb extends mysql{
  13.  
  14. public function shout($author, $shout){
  15. $this->author = $author;
  16. $this->shout = $shout;
  17. $this->data = date("Y-m-d H:i:s");
  18. $this->olddata = date("Y-m-d H:i:s", time()-7*24*60);
  19.  
  20. //$stmt = $db->prepare("DELETE FROM isb_shouts WHERE sdate < ?");
  21. //$stmt->bindValue(1, $this->olddata);
  22. //$stmt->execute();
  23.  
  24. $stmt = $db->prepare("INSERT INTO isb_shouts(sid, author, sdate, shout) VALUES(?, ?, ?, ?, ?)");
  25. $stmt->bindValue(1, '');
  26. $stmt->bindValue(2, $this->author);
  27. $stmt->bindValue(3, $this->data);
  28. $stmt->bindValue(4, $this->author);
  29. $stmt->execute();
  30. }
  31.  
  32. public function fetchshouts(){
  33. $stmt = $db->prepare("SELECT * FROM isb_shouts");
  34. $stmt->execute();
  35. while($row = $stmt->fetch()){
  36. if(EMOTICONS == 1){
  37. $emot = $db->prepare("SELECT * FROM isb_emoticons");
  38. $emot->execute();
  39.  
  40. while($emots = $emot->fetch()){
  41. $emot_sign = $emots['emoticon_sign'];
  42. $emot_image = "<img src='".$emots['emoticon_image']."' />";
  43. str_replace($emot_sign, $emot_image, $row['shout']);
  44. }
  45. }
  46. if(SBCODE == 1){
  47. $sbcode = $db->prepare("SELECT * FROM isb_sbcode");
  48. $sbcode->execute();
  49.  
  50. while($code = $sbcode->fetch()){
  51. $code_sign = $sbcode['code'];
  52. $code_html = $sbcode['codehtml'];
  53. str_replace($code_sign, $code_html, $row['shout']);
  54. }
  55. }
  56. if(CENSURE == 1){
  57. $censure = $db->prepare("SELECT * FROM isb_censore");
  58. $censure->execute();
  59.  
  60. while($word = $censure->fetch()){
  61. $word_tocensore = $word['word'];
  62. $word_replecement = $word['replacement'];
  63. str_replace($word_tocensore, $word_replecement, $row['shout']);
  64. }
  65. }
  66. }
  67. return $row['author'];
  68. return $row['sdate'];
  69. return $row['shout'];
  70. }
  71.  
  72. }
  73.  
  74.  
  75. ?>


I teraz takie pytanie:
Jak wykorzystać $db w klasie dziedziczącej po mysql?? mam na myśli sb
Go to the top of the page
+Quote Post
sajegib
post 29.01.2013, 20:58:10
Post #2





Grupa: Zarejestrowani
Postów: 352
Pomógł: 59
Dołączył: 16.01.2013

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


Skoro nie masz konstruktora w dziedziczącej klasie to zadziałał konstruktor z klasy nadrzędnej. Mogę się mylić, bo nie sprawdzę tego teraz, ale:

  1. $stmt = $db->prepare


zamień na

  1. $stmt = $this->db->prepare


I powinno grać

Ten post edytował sajegib 29.01.2013, 20:58:46
Go to the top of the page
+Quote Post
Raven1122
post 29.01.2013, 21:05:06
Post #3





Grupa: Zarejestrowani
Postów: 369
Pomógł: 2
Dołączył: 1.11.2010

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


Nie działa ;(
Go to the top of the page
+Quote Post
CuteOne
post 29.01.2013, 21:06:46
Post #4





Grupa: Zarejestrowani
Postów: 2 958
Pomógł: 574
Dołączył: 23.09.2008
Skąd: wiesz, że tu jestem?

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


  1. <?php
  2. require_once("config.php");
  3. class mysql{
  4.  
  5. protected $db;
  6.  
  7. public function __construct(){
  8. $this->db = new PDO('mysql:host='.HOST.';dbname='.DB.'', USERNAME, PASSWORD);
  9. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10. }
  11.  
  12. }
  13.  
  14.  
  15. class sb extends mysql{
  16.  
  17. public function shout($author, $shout){
  18.  
  19. $this->db->funkcja();
  20. }
  21. }
Go to the top of the page
+Quote Post
Raven1122
post 29.01.2013, 21:10:22
Post #5





Grupa: Zarejestrowani
Postów: 369
Pomógł: 2
Dołączył: 1.11.2010

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


Dalej niestety nie działa
Go to the top of the page
+Quote Post
sajegib
post 29.01.2013, 21:31:23
Post #6





Grupa: Zarejestrowani
Postów: 352
Pomógł: 59
Dołączył: 16.01.2013

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


  1. $this->db = new PDO('mysql:host='.HOST.';dbname='.DB.'', USERNAME, PASSWORD);


A tutaj zmieniłeś wartości na właściwe?
Jakiś błąd wywala?

Ten post edytował sajegib 29.01.2013, 21:31:57
Go to the top of the page
+Quote Post
Raven1122
post 29.01.2013, 21:40:32
Post #7





Grupa: Zarejestrowani
Postów: 369
Pomógł: 2
Dołączył: 1.11.2010

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


Rozwiązałem problem, ale pojawił się następny. Mianowicie ręcznie rekord mogę dodać, ale już przez AJAX się nie dodaje
Go to the top of the page
+Quote Post
CuteOne
post 29.01.2013, 21:45:48
Post #8





Grupa: Zarejestrowani
Postów: 2 958
Pomógł: 574
Dołączył: 23.09.2008
Skąd: wiesz, że tu jestem?

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


czy my wyglądamy na wróżki?
1. poczytaj o debugowaniu zmiennych np. var_dump($zmienna)
2. jeżeli coś nie działa przez ajaxa to za pomocą dragonfly (opera) lub narzędzi programistycznych (chrome) zbadaj co zwraca
3. i najważniejsze.... podawaj kod
Go to the top of the page
+Quote Post
cudny
post 30.01.2013, 00:15:31
Post #9





Grupa: Zarejestrowani
Postów: 387
Pomógł: 66
Dołączył: 31.03.2005
Skąd: Kielce

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


Cytat(CuteOne @ 29.01.2013, 21:06:46 ) *
  1. <?php
  2. require_once("config.php");
  3. class mysql{
  4.  
  5. protected $db;
  6.  
  7. public function __construct(){
  8. $this->db = new PDO('mysql:host='.HOST.';dbname='.DB.'', USERNAME, PASSWORD);
  9. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10. }
  11.  
  12. }
  13.  
  14.  
  15. class sb extends mysql{
  16.  
  17. public function shout($author, $shout){
  18.  
  19. $this->db->funkcja();
  20. }
  21. }


Zabrakło wywołania konstruktora klasy nadrzędnej.
Poniższe powinno działać:

  1. <?php
  2. require_once("config.php");
  3. class mysql{
  4.  
  5. protected $db;
  6.  
  7. public function __construct(){
  8. $this->db = new PDO('mysql:host='.HOST.';dbname='.DB.'', USERNAME, PASSWORD);
  9. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10. }
  11.  
  12. }
  13.  
  14.  
  15. class sb extends mysql{
  16. public function __construct(){
  17. parent::__construct();
  18. }
  19.  
  20. public function shout($author, $shout){
  21.  
  22. $this->db->funkcja();
  23. }
  24. }


--------------------
..::: Jak pomogłem to kliknij pomógł. Tak rzadko używacie tej opcji :( :::..
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: 19.07.2025 - 20:41