php - 如何使用扩展类的数据库连接错误 "Undefined variable: conn in ..."

标签 php mysql oop mysqli

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

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 类...

最佳答案

您必须使用 self::$conn 访问其静态

使用$this->,您只能访问“无”静态变量

更新为:

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;

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

关于php - 如何使用扩展类的数据库连接错误 "Undefined variable: conn in ...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36444596/

相关文章:

javascript - Ratchet Websockets - 检测断开连接的客户端

php - get_object_vars 根据 PHP 版本返回不同的结果

加载 CSV 文件时,PHP 在完成脚本之前终止(?)

MySQL 从今天开始选择*

python - 使用参数列表在 GUI 中创建单选按钮

php - 从 mysql 中提取链接并使其可点击?

使用 Chef 在 CentOS 上安装 MySQL 社区服务器

mysql - 连接到 MySQL 时 SonarQube 未启动

php - 函数按顺序调用

oop - Object.Do something() 或 Do something With(Object)