Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][MYSQL] Problem z wyświetlaniem
Forum PHP.pl > Forum > Przedszkole
Qerty
Mam problem. W ogóle nie chce mi się wyświetlać rekord za bazy danych. Po prostu białe tło. Jaka jest tego przyczyna. Z klasą chyba jest ok wszystko.

  1. <?php
  2.  
  3.    class show {
  4.  
  5.    public $name;
  6.  
  7.    public function get() {
  8.    
  9.    $pdo= new PDO('mysql:host=localhost;dbname=trial', 'root', 'mentormiszcz1e');
  10.    $stmt= $pdo -> prepare('SELECT nick,password FROM things WHERE id=:id');
  11.    $stmt-> bindvalue (':id', PDO::PARAM_STR);
  12.    $stmt-> execute();
  13.    $stmt-> closeCursor();
  14.    } // end get();
  15.    
  16.    public function showz() {
  17.    
  18.    while ($row= $stmt-> fetch()) {
  19.    
  20.    $this-> name= $row['nick'];
  21.    return $this->name;
  22.    
  23.    } // end while
  24.    
  25.    } // end showz();
  26.    
  27. } // end class
  28.  
  29. $show= new show;
  30. $show-> get();
  31. echo $show -> name;
  32.  
  33. ?>
webdice
Zmienna stmt nie istnieje w metodzie showz, włącz sobie pełne raportowanie o błędach.
Qerty
Co muszę zrobić w takim razie żeby zmienna stmt należała? W ogóle zapomniałem programowania obiektowego dlatego pytam. Próbuję też włączyć raportowanie błędów przez: error_reporting(E_ALL); i ini_set('display_errors',1); , ale dalej pokazuje pustą stronę.
webdice
  1. <?php
  2. $this -> stmt= $pdo -> prepare('SELECT nick,password FROM things WHERE id=:id');
  3. ?>


Resztę sobie dorysuj sam winksmiley.jpg. Wywołanie klasy pozostawiam już Twojej inwencji.
Qerty
Ok, ale jak zrobiłem tak to pisze, ze nie zdefiniowana zmienna. Dokładanie tak to wygląda: Co teraz znowu jest źle, nie wiem.
Cytat
Notice: Undefined variable: stmt in C:\Program Files\WebServ\httpd-users\test\trial\sign.php on line 14

Fatal error: Call to a member function bindvalue() on a non-object in C:\Program Files\WebServ\httpd-users\test\trial\sign.php on line 14


  1. <?php
  2.  
  3. ini_set('display_errors',1);
  4.  
  5.    class show {
  6.  
  7.    public $name;
  8.  
  9.    public function get() {
  10.    
  11.    $pdo= new PDO('mysql:host=localhost;dbname=trial', 'root', 'mentormiszcz1e');
  12.    $this-> stmt= $pdo -> prepare('SELECT nick,password FROM things WHERE id=:id');
  13.    $stmt-> bindvalue (':id', PDO::PARAM_STR);
  14.    $stmt-> execute();
  15.    $stmt-> closeCursor();
  16.    } // end get();
  17.    
  18.    public function showz($name) {
  19.  
  20.    while ($row= $stmt-> fetch()) {
  21.    $this-> name= $row['nick'];
  22.    return $this->name;
  23.    }
  24.    
  25.    }
  26.    
  27. } // end class
  28.  
  29. $show= new show;
  30. $show-> get();
  31. echo $show -> name;
  32.  
  33. ?>
webdice
A co z reszta stmt?
Qerty
Nie wiem o co chodzi tutaj. Niby wszystko Ok jest, zmienna stmt istnieje, ale dalej się to pojawia. Reszta jest ok, bo nic nie wyskakuje. Zapomniałem powiedzieć że jak wyświetlanie robię to samo z użyciem wątka (try) o działa bez problemów.
tomm
zdefiniuj sobie zmienną $stmt jako atrybut klasy np. protected $stmt

a później odwołuj się do niej $this->stmt
Qerty
Nic to nie daje. Jak daję z protected to się znowu pojawia:
Cytat
Fatal error: Call to undefined function bindvalue() in C:\Program Files\WebServ\httpd-users\test\trial\sign.php on line 15


Już sam nie wiem co robić.
webdice
Pomyśl trochę sam.

  1. <?php
  2. $this-> stmt = $pdo -> prepare('SELECT nick,password FROM things WHERE id=:id');
  3. $this-> stmt -> bindvalue (':id', PDO::PARAM_STR);
  4. $this-> stmt -> execute();
  5. $this-> stmt -> closeCursor();
  6. ?>


To samo w drugiej metodzie.
Qerty
Dzięki webdice .Działa,ale wcześniej już sobie poradziłem smile.gif Trochę pomieszałem sprawę z metodami, dlatego nie chciało wyświetlać. Teraz chodzi.

  1. <?php
  2.  
  3. ini_set('display_errors',1);
  4.  
  5.  
  6.    class user {
  7.  
  8.    public $name;
  9.    
  10.  
  11.    public function get() {
  12.    
  13.    $pdo = new pdo ('mysql:host=localhost;dbname=trial','root','mentormiszcz1e');
  14.    $stmt= $pdo-> prepare("SELECT nick FROM things WHERE id=:id");
  15.    $stmt-> bindvalue (':id', PDO::PARAM_STR);
  16.    $stmt-> execute();
  17.    
  18.    while ($row= $stmt-> fetch()) {
  19.    
  20.    $this-> name= $row['nick'];
  21.    
  22.    } // end while
  23.    
  24.    } // end function
  25.  
  26.    public function show() {
  27.    
  28.    return $this->name;
  29.    
  30.    }
  31.    
  32. } // end class
  33.  
  34. $user= new user;
  35. $user-> get();
  36. echo $user->name;
  37.  
  38. ?>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.