【问题标题】:How to use db connection from extended class error " Undefined variable: conn in ..."如何使用扩展类错误“未定义变量:conn in ...”中的数据库连接
【发布时间】:2016-07-26 11:17:27
【问题描述】:

尝试为所有数据库连接使用一个数据库类文件,通过扩展该类 如何...

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

运行dashboard.php时抛出错误:

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

请告诉我如何在另一个 php 文件和类中使用 mydb 类...

【问题讨论】:

    标签: php mysql oop mysqli


    【解决方案1】:

    你必须用self::$conn访问它的静态

    $this-&gt; 只能访问“无”静态变量

    更新到:

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

    【讨论】:

    • 但现在显示错误“致命错误:在第 8 行的 C:\program data2\xampp\htdocs\engine\admin\cms.class.php 中调用一个成员函数 query() on null”
    • 能否请您更新代码或显示您所做的更改?
    • 查看我的帖子并更改文件 mydb.php 和 cms.class.php,您必须在对象的一侧使用静态变量 self::$ 而不是 $this-&gt;
    猜你喜欢
    • 2015-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 2018-09-02
    • 2014-06-19
    • 2020-10-29
    • 1970-01-01
    相关资源
    最近更新 更多