Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Problem ze sterownikiem BD
Forum PHP.pl > Forum > Przedszkole
-raf2001-
Witam mam problem ze sterownikiem bazy danych, oto on:
  1. <?
  2.  class DataBase {
  3.  public $connection; 
  4.  public $database; 
  5.  public $query; 
  6.  public $result = array(); 
  7.  public $records = array(); 
  8.  public $queries = 0; 
  9.  public $lastId; 
  10.  
  11.  private static $oInstance = false;
  12.  
  13.  private function __construct(){}
  14.  
  15.  public static function Instance(){
  16.  if(self::$oInstance == false){
  17.  self::$oInstance = new DataBase();
  18.  }
  19.  return self::$oInstance;
  20.  }
  21.  
  22.  public function error() { 
  23.  echo '<b>'.mysql_errno().'</b> : '.mysql_error().'<br>'; 
  24.  } 
  25.  
  26.  public function connect($MYSQL_Host, $MYSQL_User, $MYSQL_Pass, $MYSQL_Base) { 
  27.  if($MYSQL_Host && $MYSQL_User && $MYSQL_Pass && $MYSQL_Base){
  28.  $this->connection = mysql_pconnect($MYSQL_Host, $MYSQL_User, $MYSQL_Pass);
  29.  if($this->connection){
  30.  $this->database = mysql_select_db($MYSQL_Base);
  31.  if($this->database){
  32.  return TRUE;
  33.  } else {
  34.  return FALSE;
  35.  $this->error();
  36.  mysql_close($this->connection);
  37.  }
  38.  } else {
  39.  return FALSE;
  40.  $this->error();
  41.  }  
  42.  } 
  43.  } 
  44.  
  45.  public function close() {
  46.  if($this->connection){
  47.  mysql_close($this->connection);
  48.  unset($this->result, $this->database, $this->connection);
  49.  return TRUE;
  50.  } else {
  51.  $this->error();
  52.  return FALSE;
  53.  }
  54.  } 
  55.  
  56.  public function query($query) { 
  57.  if($query && $this->database){
  58.  if($this->result != ''){
  59.  $this->result = '';
  60.  }
  61.  $this->result = mysql_query($query);
  62.  $this->lastId = mysql_insert_id();
  63.  $this->queries++;
  64.  return $this->result;
  65.  } else {
  66.  $this->error();
  67.  return FALSE;
  68.  }
  69.  } 
  70.  
  71.  public function fetch_array($arrayHandle=0) { 
  72.  if($this->database && $this->result) { 
  73.  if($arrayHandle==0){
  74. $this->records = mysql_fetch_array($this->result, MYSQL_ASSOC); 
  75.  return $this->records;
  76.  } else {
  77. $this->records = mysql_fetch_array($arrayHandle, MYSQL_ASSOC); 
  78.  return $this->records;
  79.  }
  80.  } else {
  81.  $this->error();
  82.  }
  83.  } 
  84.  
  85.  public function fetch_row($rowHandle=0) { 
  86.  if($this->database && $this->result) { 
  87.  if($arrayHandle==0){
  88. $this->records = mysql_fetch_row($this->result); 
  89.  return $this->records;
  90.  } else {
  91. $this->records = mysql_fetch_row($arrayHandle); 
  92.  return $this->records;
  93.  }
  94.  } else {
  95.  $this->error();
  96.  }
  97.  } 
  98.  
  99.  public function num_rows($resultHandle=0) { 
  100.  if($arrayHandle==0){
  101.  $num_rows = mysql_num_rows($this->result);  
  102.  return $num_rows;  
  103.  } else {
  104.  $num_rows = mysql_num_rows($resultHandle);
  105.  return $num_rows;  
  106.  }
  107.  } 
  108.  }
  109. ?>


i próbuje sie odwołać w klasie do obsługi newsów w taki sposób:

  1. <?php
  2. class News{
  3. public $DS;
  4.  
  5. /*
  6.  * Metoda konstrukcyjna z odniesieniem do źrodla danych
  7.  */
  8. public function __construct($DS){
  9. $this->DS = $DS;
  10. ...
  11. ...
  12.  public function get_all($news_limit){
  13. $news_all = $this->DS->query('SELECT * FROM cms_news ORDER BY news_id DESC LIMIT '.$news_limit);
  14. return $news_all;
  15. }
  16. ?>

wówczas wywala taki błąd:
Fatal error: Call to a member function query() on a non-object in C:\Program Files\WebServ\httpd\modules\News.php on line 34
plik index wygląd tak:
  1. <?php
  2. $DBase = DataBase::Instance(); //2
  3.  $DBase = $DBase->connect($MYSQL_Host, $MYSQL_User, $MYSQL_Pass, $MYSQL_Base);
  4. //----------------------------------------------------
  5.  $News = new News($DBase);
  6. ?>

co może być przyczyną tego że nie widzi guery. oraz czy sterownik jest poprawnie napisany?
nospor
nie:
  1. <?php
  2. $DBase = $DBase->connect($MYSQL_Host, $MYSQL_User, $MYSQL_Pass, $MYSQL_Base);
  3. ?>

a:
  1. <?php
  2. $DBase->connect($MYSQL_Host, $MYSQL_User, $MYSQL_Pass, $MYSQL_Base);
  3. ?>

Napisywales obiekt rezultatem jego metody.
-raf2001-
smile.gif mały błąd, a tyle sie nameczyłem nad analizowaniem kodu...a to taka drobnostka....dzięki. miałeś racje
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.