Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

3 Stron V   1 2 3 >  
Reply to this topicStart new topic
> [PHP]function fetch(), Problem z kodem.
-koskiplay-
post
Post #1





Goście







Witam, jest to mój pierwszy post na tym forum.
Otóż zabrałem się za tworzenie cmsa. Korzystam z książki wydawnictwa Helion "Tworzenie systemów cms przy użyciu PHP i JQuery".
Niestety napotkałem błąd.
Wszystko wyjaśniam.
Oto sam błąd:
  1. Fatal error: Call to a member function fetch() on a non-object in C:\WebServ\httpd\websites\CMS\mt.incs\basics.php on line 23

A tutaj kod basics.php
  1. <?php
  2. function __autoload($name) {
  3. require $name.'.php';
  4. }
  5. function dbInit(){
  6. if(isset($GLOBALS['db']))return $GLOBALS['db'];
  7. global $DBVARS;
  8. $db=new PDO('mysql:host='.$DBVARS['localhost'].';dbname='.$DBVARS['cmsdb'],$DBVARS['root'],$DBVARS['']);
  9. $db->query('SET NAMES utf8');
  10. $db->num_queries=0;
  11. $GLOBALS['db']=$db;
  12. return $db;
  13. }
  14. function dbQuery($query){
  15. $db=dbInit();
  16. $q=$db->query($query);
  17. $db->num_queries++;
  18. return $q;
  19. }
  20. function dbRow($query) {
  21. $q = dbQuery($query);
  22. return $q->fetch(PDO::FETCH_ASSOC);
  23. }
  24. define('SCRIPTBASE', $_SERVER['DOCUMENT_ROOT'] . '/');
  25. require SCRIPTBASE . '.private/config.php';
  26. if(!defined('CONFIG_FILE'))define('CONFIG_FILE',SCRIPTBASE.'.private/config.php');
  27. set_include_path(SCRIPTBASE.'mt.php_classes'.PATH_SEPARATOR.get_include_path());

Oraz plik page.php w folderze mt.php_classes.
  1. <?php
  2. class Page{
  3. static $instances = array();
  4. static $instancesByName = array();
  5. static $instancesBySpecial = array();
  6. function __construct($v,$byField=0,$fromRow=0,$pvq=0){
  7. # byField: 0=ID; 1=Name; 3=special
  8. if (!$byField && is_numeric($v)){ // wg identyfikatora
  9. $r=$fromRow?$fromRow:($v?dbRow("select * from pages where id=$v limit 1"):array());
  10. }
  11. else if ($byField == 1){ // wg nazwy
  12. $name=strtolower(str_replace('-','_',$v));
  13. $fname='page_by_name_'.md5($name);
  14. $r=dbRow("select * from pages where name like '".addslashes($name)."' limit 1");
  15. }
  16. else if ($byField == 3 && is_numeric($v)){ // wg pola special
  17. $fname='page_by_special_'.$v;
  18. $r=dbRow("select * from pages where special&$v limit 1");
  19. }
  20. else return false;
  21. if(!count($r || !is_array($r)))return false;
  22. if(!isset($r['id']))$r['id']=0;
  23. if(!isset($r['type']))$r['type']=0;
  24. if(!isset($r['special']))$r['special']=0;
  25. if(!isset($r['name']))$r['name']='NO NAME SUPPLIED';
  26. foreach ($r as $k=>$v) $this->{$k}=$v;
  27. $this->urlname=$r['name'];
  28. $this->dbVals=$r;
  29. self::$instances[$this->id] =& $this;
  30. self::$instancesByName[preg_replace('/[^a-z0-9]/','-',strtolower($this->urlname))] =& $this;
  31. self::$instancesBySpecial[$this->special] =& $this;
  32. if(!$this->vars)$this->vars='{}';
  33. $this->vars=json_decode($this->vars);
  34. }
  35. static public function getInstance($id=0,$fromRow=false,$pvq=false){
  36. if (!is_numeric($id)) return false;
  37. if (!@array_key_exists($id,self::$instances)) self::$instances[$id]=new Page($id,0,$fromRow,$pvq);
  38. return self::$instances[$id];
  39. }
  40. static public function getInstanceByName($name=''){
  41. $name=strtolower($name);
  42. $nameIndex=preg_replace('#[^a-z0-9/]#','-',$name);
  43. if(@array_key_exists($nameIndex,self::$instancesByName))return self::$instancesByName[$nameIndex];
  44. self::$instancesByName[$nameIndex]=new Page($name,1);
  45. return self::$instancesByName[$nameIndex];
  46. }
  47. static public function getInstanceBySpecial($sp=0){
  48. if (!is_numeric($sp)) return false;
  49. if (!@array_key_exists($sp,$instancesBySpecial)) $instancesBySpecial[$sp]=new Page($sp,3);
  50. return $instancesBySpecial[$sp];
  51. }
  52. }

Proszę o pomoc, nie potrafię sam sobie z tym poradzić. (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
Turson
post
Post #2





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

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


  1. function dbRow($query) {
  2. $q = dbQuery($query);
  3. return $q->fetch(PDO::FETCH_ASSOC);
  4. }

daj echo $q i zobacz co tam jest.

Generalnie tak to powinno wyglądać, bo zgubiłeś odwołanie do PDO przez $db:
  1. $q = $db->query("SELECT * FROM tabela");
  2. return $q->fetch();


Ten post edytował Turson 4.12.2013, 17:04:10
Go to the top of the page
+Quote Post
-koskiplay-
post
Post #3





Goście







Cytat(Turson @ 4.12.2013, 17:03:11 ) *
daj echo $q i zobacz co tam jest.

Echo wywala mi napis "Błąd 404" z pliku Page.php.

A po zmianie na:
  1. $q = $db->query("SELECT * FROM tabela");
  2. return $q->fetch();


Widnieje błąd:
  1. Fatal error: Call to a member function query() on a non-object in C:\WebServ\httpd\websites\CMS\mt.incs\basics.php on line 22
Go to the top of the page
+Quote Post
Turson
post
Post #4





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

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


Połącz się z bazą w tej funkcji $db=dbInit();


  1. $q = $db->query("SELECT * FROM tabela");

A zmieniłeś w ogóle na własne dane to zapytanie?

Ten post edytował Turson 4.12.2013, 17:26:47
Go to the top of the page
+Quote Post
-koskiplay-
post
Post #5





Goście







Tak, zmieniłem. Nie chciałem po prostu edytować cytatu.
Nadal nie działa, to samo powiadomienie, nie umiem dojść do rozwiązania sam.
Go to the top of the page
+Quote Post
-koskiplay-
post
Post #6





Goście







Wrzucam dodatkowo plik index.php. Mam nadzieje, że to pomoże w problemie.
  1. <?php
  2. //{wspólne zmienne i funkcje
  3. include_once('mt.incs/common.php');
  4. $page=isset($_REQUEST['page'])?$_REQUEST['page']:'';
  5. $id=isset($_REQUEST['id'])?(int)$_REQUEST['id']:0;
  6. //}
  7. //{sprawdzenie identyfikatora bieżącej strony
  8. if(!$id){
  9. if($page){ //wczytanie wg nazwy
  10. $r=Page::getInstanceByName($page);
  11. if($r && isset ($r->id))$id=$r->id;
  12. unset($r);
  13. }
  14. if(!$if){ //albo wczytanie wg zawartości pola special
  15. $special=1;
  16. if(!$page){
  17. $r=Page::getInstanceBySpecial($special);
  18. if($r && isset($r->id))$id=$r->id;
  19. unset($r);
  20. }
  21. }
  22. }
  23. //}
  24. //{ wczytywanie danych na stronę
  25. if ($id){
  26. $PAGEDATA=(isset($r) &&$r)? $r: Page::getInstance($id);
  27. }
  28. else {
  29. echo 'Błąd 404';
  30. }
  31. //}
  32. echo $PAGEDATA->body;
Go to the top of the page
+Quote Post
-koskiplay-
post
Post #7





Goście







Pomoże ktoś?(IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
werdan
post
Post #8





Grupa: Zarejestrowani
Postów: 354
Pomógł: 100
Dołączył: 14.11.2013
Skąd: Płock

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


  1. function dbRow($query) {
  2. $q = dbQuery($query);
  3.  
  4. var_dump($q);die(); // wklej wynik z tego
  5.  
  6. return $q->fetch(PDO::FETCH_ASSOC);
  7. }
  8.  
  9.  
  10.  
Go to the top of the page
+Quote Post
-koskiplay-
post
Post #9





Goście







Wyskoczyło mi:
  1. bool(false)

Co to oznacza?
Go to the top of the page
+Quote Post
werdan
post
Post #10





Grupa: Zarejestrowani
Postów: 354
Pomógł: 100
Dołączył: 14.11.2013
Skąd: Płock

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


Oznacza, że dbQuery($query); nic nie zwraca, a powinno.
Go to the top of the page
+Quote Post
-koskiplay-
post
Post #11





Goście







Jak to rozwiązać? Problem jest w pliku Page.php?
Go to the top of the page
+Quote Post
werdan
post
Post #12





Grupa: Zarejestrowani
Postów: 354
Pomógł: 100
Dołączył: 14.11.2013
Skąd: Płock

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


  1.  
  2. function dbInit(){
  3. if(isset($GLOBALS['db']))return $GLOBALS['db'];
  4. global $DBVARS;
  5. $db=new PDO('mysql:host='.$DBVARS['localhost'].';dbname='.$DBVARS['cmsdb'],$DBVARS['root'],$DBVARS['']);
  6. var_dump("new PDO", $db);
  7. $db->query('SET NAMES utf8');
  8. $db->num_queries=0;
  9. $GLOBALS['db']=$db;
  10. var_dump("PDO", $db);
  11. return $db;
  12. }
  13. function dbQuery($query){
  14. $db=dbInit();
  15. var_dump("dbInit", $db);
  16. $q=$db->query($query);
  17. var_dump("query", $q); die();
  18. $db->num_queries++;
  19. return $q;
  20. }
  21.  



To zmien i wklej tu wynik.
Go to the top of the page
+Quote Post
-koskiplay-
post
Post #13





Goście







Poprawnie?
  1. <?php
  2. function __autoload($name) {
  3. require $name.'.php';
  4. }
  5. function dbInit(){
  6. if(isset($GLOBALS['db']))return $GLOBALS['db'];
  7. global $DBVARS;
  8. $db=new PDO('mysql:host='.$DBVARS['localhost'].';dbname='.$DBVARS['cmsdb'],$DBVARS['root'],$DBVARS['']);
  9. var_dump("new PDO", $db);
  10. $db->query('SET NAMES utf8');
  11. $db->num_queries=0;
  12. $GLOBALS['db']=$db;
  13. var_dump("PDO", $db);
  14. return $db;
  15. }
  16. function dbQuery($query){
  17. $db=dbInit();
  18. var_dump("dbInit", $db);
  19. $q=$db->query($query);
  20. var_dump("query", $q); die();
  21. $db->num_queries++;
  22. return $q;
  23. }
  24. function dbRow($query) {
  25. $q = dbQuery($query);
  26. var_dump($q);die(); // wklej wynik z tego
  27. return $q->fetch(PDO::FETCH_ASSOC);
  28. }
  29. define('SCRIPTBASE', $_SERVER['DOCUMENT_ROOT'] . '/');
  30. require SCRIPTBASE . '.private/config.php';
  31. if(!defined('CONFIG_FILE'))define('CONFIG_FILE',SCRIPTBASE.'.private/config.php');
  32. set_include_path(SCRIPTBASE.'mt.php_classes'.PATH_SEPARATOR.get_include_path());

  1. string(7) "new PDO" object(PDO)#2 (0) { } string(3) "PDO" object(PDO)#2 (1) { ["num_queries"]=> int(0) } string(6) "dbInit" object(PDO)#2 (1) { ["num_queries"]=> int(0) } string(5) "query" bool(false)
Go to the top of the page
+Quote Post
werdan
post
Post #14





Grupa: Zarejestrowani
Postów: 354
Pomógł: 100
Dołączył: 14.11.2013
Skąd: Płock

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


OK, teraz pousuwaj wszystkie var_dump() i die()


  1. function dbRow($query) {
  2.  
  3. var_dump("query",$query);die(); // wklej wynik z tego
  4. $q = dbQuery($query);
  5. return $q->fetch(PDO::FETCH_ASSOC);
  6. }
  7.  
  8.  
  9.  
Go to the top of the page
+Quote Post
-koskiplay-
post
Post #15





Goście







Usunięte.
  1. <?php
  2. function __autoload($name) {
  3. require $name.'.php';
  4. }
  5. function dbInit(){
  6. if(isset($GLOBALS['db']))return $GLOBALS['db'];
  7. global $DBVARS;
  8. $db=new PDO('mysql:host='.$DBVARS['localhost'].';dbname='.$DBVARS['cmsdb'],$DBVARS['root'],$DBVARS['']);
  9. $db->query('SET NAMES utf8');
  10. $db->num_queries=0;
  11. $GLOBALS['db']=$db;
  12. return $db;
  13. }
  14. function dbQuery($query){
  15. $db=dbInit();
  16. $q=$db->query($query);
  17. $db->num_queries++;
  18. return $q;
  19. }
  20. function dbRow($query) {
  21. $q = dbQuery($query);
  22. return $q->fetch(PDO::FETCH_ASSOC);
  23. }
  24. define('SCRIPTBASE', $_SERVER['DOCUMENT_ROOT'] . '/');
  25. require SCRIPTBASE . '.private/config.php';
  26. if(!defined('CONFIG_FILE'))define('CONFIG_FILE',SCRIPTBASE.'.private/config.php');
  27. set_include_path(SCRIPTBASE.'mt.php_classes'.PATH_SEPARATOR.get_include_path());
  28. ?>


Komunikat taki jak na początku, tj.
  1. Fatal error: Call to a member function fetch() on a non-object in C:\WebServ\httpd\websites\CMS\mt.incs\basics.php on line 23
Go to the top of the page
+Quote Post
werdan
post
Post #16





Grupa: Zarejestrowani
Postów: 354
Pomógł: 100
Dołączył: 14.11.2013
Skąd: Płock

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


Wklej co powyzej podałem.
Go to the top of the page
+Quote Post
-Gość-
post
Post #17





Goście







Wynik:
  1. string(5) "query" string(43) "select * from pages where special&1 limit 1"
Go to the top of the page
+Quote Post
werdan
post
Post #18





Grupa: Zarejestrowani
Postów: 354
Pomógł: 100
Dołączył: 14.11.2013
Skąd: Płock

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


W pliku mt.php_classes zmien tą linie na poprawną:

  1. $r=dbRow("select * from pages where special&$v limit 1");


Tam zamiast & powinno być chyba =
Go to the top of the page
+Quote Post
-koskiplay-
post
Post #19





Goście







mt.php_classes to folder, w nim znajduje się plik page.php. (Jest on w pierwszym moim poście)
Ta linijka zawiera =, nie &. Więc tam jest chyba dobrze
Go to the top of the page
+Quote Post
werdan
post
Post #20





Grupa: Zarejestrowani
Postów: 354
Pomógł: 100
Dołączył: 14.11.2013
Skąd: Płock

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


Tak faktycznie page.php

  1. if (!$byField && is_numeric($v)){ // wg identyfikatora
  2. $r=$fromRow?$fromRow:($v?dbRow("select * from pages where id=$v limit 1"):array());
  3. }
  4. else if ($byField == 1){ // wg nazwy
  5. $name=strtolower(str_replace('-','_',$v));
  6. $fname='page_by_name_'.md5($name);
  7. $r=dbRow("select * from pages where name like '".addslashes($name)."' limit 1");
  8. }
  9. else if ($byField == 3 && is_numeric($v)){ // wg pola special
  10. $fname='page_by_special_'.$v;
  11. $r=dbRow("select * from pages where special&$v limit 1");
  12. }


Zobacz co masz!
Linia 11 w kodzie powyzej
$r=dbRow("select * from pages where special&$v limit 1");

Zmień & na =

Ten post edytował werdan 5.12.2013, 18:40:15
Go to the top of the page
+Quote Post

3 Stron V   1 2 3 >
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: 23.08.2025 - 08:38