致命错误:未捕获错误:在null

时间:2017-10-08 11:20:51

标签: php

I got stuck for hours in the code below. I don't know how I can fix this error.
  

致命错误:未捕获错误:在C:\ xampp \ htdocs \ code \ abc \ Services \ Backend.php中调用null上的成员函数query():16堆栈跟踪:#0 C:\ xampp \ htdocs \ code \ abc \ v1 \ Api.php(9):后端 - > userLogin()#1 {main}在第16行的C:\ xampp \ htdocs \ code \ abc \ Services \ Backend.php中抛出。

DB.php文件

<?php
$Servername = "localhost";
$Username   = "root";
$Password   = "";
$dbName     = "android";
// multi
$conn       = new mysqli($Servername, $Username, $Password, $dbName);
?>

Service.php文件

<?php

 //Class Services
 class Services
 {
   //Variable to store database link
   public $conn;

   //Class constructor
   function __construct()
   {
     $this->connect();
   }

   //This method will connect to the database
   function connect()
   {
     //Including the constants.php file to get the database constants
     include_once dirname(__FILE__) . '/linkDB.php';

     // //Checking if any error occured while connecting
     if ($conn->connect_errno) {
     echo "Failed to connect to MySQL: " . $conn->mysqli_connect_error();
   }
     //finally returning the connection link
     return $this->conn;
   }
  }
?>

Backend.php文件

<?php

/**
 * to perform all the operation needed
 */
class Backend extends Services
{
    function __construct()
    {
        parent::__construct();
    }

    # always login the user in [WHERE user_id='{$id}' AND password='{$password}']  [$id , $password]
    public function userLogin()
    {
        $stmt   = "SELECT * FROM heroes ";
        $retval = $this->conn->query($stmt);
        $data   = $retval->fetch_array();
        $row    = $retval->num_rows;
        if ($row == 1) {
            return true;
        } else {
            return false;
        }
        print_r($this->conn);
    }

    # register a new lecturer or student[$id , $phone , $email , $type, $password , $rpassword]
    // public function userRegister($name, $realname, $rating, $teamaffiliation)
    // {
    //   $stmt = "INSERT INTO heroes(name, realname, rating, teamaffiliation)
    //            VALUES('{$name}', '{$realname}', '{$rating}', '{$teamaffiliation}')";
    //   $retval = $this->conn->query($stmt);
    //   // $retval = mysqli_query($this->conn , $stmt);
    //   var_dump($this->conn);
    //   if($retval){
    //     return true;
    //   }else {
    //     return false;
    //   }
    //
    // }

    # make a new order
    //  public function accReset($id, $email){
    //     $stmt = "UPDATE users_tbl SET username='{$user}' AND user_password='{$pass}' WHERE user_id={$uid}";
    //     $retval = $this->conn->query($stmt);
    //     if($retval)
    //     return true;
    //     return false;
    // }

    //
    // # know your order history
    // public function OrderHistory($id)
    // {
    //   # code...
    // }
    //
    // # hlogout the user now...
    // public function Logout()
    // {
    //   session_start();
    //   session_unset();
    //   session_destroy();
    // }
    //
    // public function hash($password)
    // {
    //
    // }


}
?>

LinkDB.php文件

<?php require_once '../../startups/DB.php'; ?>

Api.php文件

<?php
   require_once '../Services/Services.php';
   require_once '../Services/Backend.php';

  //  $api = new Services;
   $api = new Backend;

  //  $api->userRegister('YOUR', 'Killersbeans', '2', 'X-men');
   $api->userLogin();
 ?>

0 个答案:

没有答案
相关问题