dsbgltg159136540 2016-04-06 07:44
浏览 94

如何从扩展类错误“未定义变量:conn in ...”中使用数据库连接

Trying to use one database class file for all db connection, by extend that class how to...

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();
?>

when running dashboard.php throws errors:

" 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 " 

please tell me the ways how to use mydb class in another php files and class...

  • 写回答

1条回答 默认 最新

  • doulin6448 2016-04-06 07:47
    关注

    You must access with self::$conn its static

    with $this-> you access only "none" static variables

    Update to:

    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;
    
    .........
    ........... ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?