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

when trying to open a extended class in a another class error occurs..

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

Please check this code

throwing error: Fatal error: Call to a member function query() on null in C:..\engine\admin\cms.class.php on line 7

extended class mydb in login.class.php and cms.class.php how to use connection variable class in multiple pages/classes..

Answers


You would need to call parent::__construct(); in cms.class.php

<?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 );
        // ....
    }
}

Need Your Help

Why do I have to define the top-level parameter in JAGS, and how?

r jags

According to the user manual of r-jags (section Compilation):

Selenium, exception on getting a file in IE using WebClient

internet-explorer selenium webclient setcookie webclient-download

I have a Selenium test that should download a file and check if it contains something. The test run ok in Firefox and Chrome but in IE 11 I get an exception.