php OOP 错误 : Call to a member function query() on null in c:blah/bla/. ./\engine\admin\cms.class.php 第 7 行

标签 php mysql database oop mysqli

当试图在另一个类中打开扩展类时发生错误..

mydb.php

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

?>

login.class.php

 <?php
include_once __DIR__.'\..\mydb.php';
class vishnuADMIN extends mydb {
    public function login($uname,$password){
        $uname=$this->conn->real_escape_string($uname);
        $password=$this->conn->real_escape_string($password);

    $password="s4l8".sha1($password);
    $q="select * from user where uname='$uname' and password='$password'";
    $res=$this->conn->query($q);
       if(mysqli_num_rows($res)){
//           (!isset($_SESSION))? session_start() :"";
           $_SESSION["user"]=$uname;
           return true;
       }else{
           return false;
       }
    }
    public function register($uname,$password){
        $password="s4l8".sha1($password);
       echo $q="INSERT INTO user(uname,password) VALUES ('$uname','$password')";
        $res=$this->conn->query($q);
        if($res){
            (!isset($_SESSION))?session_start():"";
            $_SESSION["user"]=$uname;
            return true;
        }else{
            return false;
        }

    }
      public function logincheck(){
//        (!isset($_SESSION))?session_start():"";
        if(isset($_SESSION["user"])){
            return true;
        }else{
             return false;
        }
    }
    public function logout(){
//        (!isset($_SESSION))?session_start():"";
        session_unset();
    }
}

//$obj=new vishnuADMIN();
//if($obj->logincheck()){
//    echo "hai";
//}

cms.class.php

 <?php 
class paginator_vishnukumar extends mydb{ 
    private $_limit,$_page,$_query,$_total,$_conn;
    public function __construct( $query ) {
    $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;
 /*
        some blah blah codes...

        */
    }
    public function createLinks( $links, $list_class ) {
    if ( $this->_limit == 'all' ) {
        return '';
    /*
        some blah blah codes...

        */
    }

    return $html;
}
}
?>

admin.php

 <?php 
require 'engine/vishnuHTML.class.php';
require 'engine/admin/login.class.php';
$html=new vishnuHTML();
 (!isset($_SESSION))?session_start():"";
$html->head();
$html->navigation();
$log=new vishnuADMIN();
/* vishnuADMIN::login("vishnu","vishnukumar"); */
if(isset($_POST['submit'])){
    if($log->login($_POST['uname'],$_POST['password'])){
logedin();
    }else{
        echo "invalid";
    }
}

if($log->logincheck()){
 logedin();
}else{
    loginform();
}

?>



<?php



$html->footer();
function loginform(){
?>
<div class="row">

    <div class="login columns">
    <div class="four column">
<form method="post" action="">
<label>User name</label>
<input type="text" name="uname"><br>
<label>Password</label>
<input type="password" name="password"><br>
<input type="submit" value="go" name="submit"><br>
</form>
</div>
</div>
</div>
<?php
}


function logedin(){
        require 'engine/admin/cms.class.php';
    $limit      = ( isset( $_GET['limit'] ) ) ? $_GET['limit'] : 25;
    $page       = ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1;
    $links      = ( isset( $_GET['links'] ) ) ? $_GET['links'] : 7;
    $query      = "SELECT * FROM pages";
    $Paginator  = new paginator_vishnukumar( $query );
    $results    = $Paginator->getData( $page, $limit );
}
?>

请检查此代码

抛出错误: fatal error :在第 7 行的 C:..\engine\admin\cms.class.php 中对 null 上的成员函数 query() 调用

login.class.php 和 cms.class.php 中的扩展类 mydb 如何在多个页面/类中使用连接变量类..

最佳答案

您需要在 cms.class.php 中调用 parent::__construct();

<?php 
class paginator_vishnukumar extends mydb{ 
    private $_limit,$_page,$_query,$_total,$_conn;

    public function __construct( $query ) {

        parent::__construct(); // <-- will initiate the connection

        $this->_query = $query;
        $rs= $this->conn->query( $this->_query );
        // ....
    }
}

关于php OOP 错误 : Call to a member function query() on null in c:blah/bla/. ./\engine\admin\cms.class.php 第 7 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36418497/

相关文章:

php - MySQL:使用多个字段检查多个表中的出现情况

mysql - 根据已知日期时间随机更新日期时间字段

java - 在 URL 中公开数据库内部 ID 是一种不好的做法吗?

php - 防止被同时请求所困扰的最佳方法是什么

php - 在不使用 WHERE 的情况下显示用户的特定记录

php - 难以根据表单中的用户输入更改 PHP 文档中的 SQL 查询

mysql - 访问数据库的电脑上是否也需要安装MySql?

php - 在 "ORDER BY"子句之后是否可以放置任何可能带来安全风险的内容?

php - "detailIndex"的 Twig 错误项目 "Array"不存在于

mysql - Ansible mysql 授权