2016-04-06 11 views
0

tüm db bağlantısı için bir veritabanı sınıfı dosyası kullanmak çalışılıyor "Tanımsız değişken ... lütfen Conn" nasıl ...genişletilmiş sınıf hatadan db bağlantısını nasıl kullanılır: bu sınıfı genişletir tarafından,

mydb.php

<?php 

class mydb{ 
    public static $conn; 
    public function __construct(){ 
     $this->conn=new mysqli("localhost","root","","akshaya"); 
     if(!$this->conn){ 
      echo "mysql connection error"; 
     }else{ 
      return $this->conn; 
     } 
    } 
} 

?> 

cms.class.php

<?php 
require_once __DIR__.'\..\mydb.php'; 
class paginator_vishnukumar extends mydb{ 
    private $_limit,$_page,$_query,$_total; 
    public function __construct($query) { 
     parent::__construct(); 
    $this->_query = $query; 
    $rs= $this->$conn->query($this->_query); 
    $this->_total = $rs->num_rows; } 
    public function getData( $page = 1,$limit = 10) { 
    $this->_limit = $limit; 
    $this->_page = $page; 

......... 
........... ?> 

dashboard.php

<?php 
require 'engine/vishnuHTML.class.php'; 
require 'engine/admin/cms.class.php'; 
$html=new vishnuHTML(); 
$html->head(); 
$html->navigation(); 
    $limit  = (isset($_GET['limit'])) ? $_GET['limit'] : 25; 
    $page  = (isset($_GET['page'])) ? $_GET['page'] : 1; 
    $links  = (isset($_GET['links'])) ? $_GET['links'] : 7; 
    $query  = "SELECT * FROM posts"; 
    $Paginator = new paginator_vishnukumar($query); 
    $results = $Paginator->getData($page, $limit); 
?> 
<div class="row"> 
<div class="columns"></div> 
</div> 

<?php 
$html->footer(); 
?> 

inci dashboard.php çalıştırırken satırlar hataları:

" Notice: Undefined variable: conn in C:\program data2\xampp\htdocs\engine\admin\cms.class.php on line 8 

Fatal error: Cannot access empty property in C:\program data2\xampp\htdocs\engine\admin\cms.class.php on line 8 " 

nasıl başka bir php dosya ve sınıfta mydb sınıfını kullanmak bana yollar söyleyin lütfen ...

Sen self::$conn ile erişmesi gereken

cevap

0

onun statik $this-> ile

yalnızca erişmek " none" statik değişkenler

Güncelleme için:

mydb.php

<?php 

class mydb{ 
    public static $conn; 
    public function __construct(){ 
     self::$conn=new mysqli("localhost","root","","akshaya"); 
     if(!self::$conn){ 
      echo "mysql connection error"; 
     }else{ 
      return self::$conn; 
     } 
    } 
} 

?> 

cms.class.php

<?php 
require_once __DIR__.'\..\mydb.php'; 
class paginator_vishnukumar extends mydb{ 
    private $_limit,$_page,$_query,$_total; 
    public function __construct($query) { 
     parent::__construct(); 
    $this->_query = $query; 
    $rs= self::$conn->query($this->_query); 
    $this->_total = $rs->num_rows; } 
    public function getData( $page = 1,$limit = 10) { 
    $this->_limit = $limit; 
    $this->_page = $page; 

......... 
........... ?> 
+0

ama şimdi gösteren hatası "Önemli hata: C boş bir üye işlev sorgusu() çağır: \ Program veri2 \ xampp \ htdocs motoru \ \ yönetici \ cms.class.php 8. satırda " –

+0

Lütfen kodu güncelleyebilir veya nelerin değiştiğini gösterir misiniz? –

+0

! Lütfen bana cms.class.php dosyasındaki mydb-$ bağlantısına nasıl erişebileceğimi söyleyin. –

İlgili konular