Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 致命错误:未捕获错误:调用null上的成员函数query()_Php - Fatal编程技术网

Php 致命错误:未捕获错误:调用null上的成员函数query()

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

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

DB.php文件

Service.php文件

Backend.php文件
看起来您从未将
$this->conn
设置为任何值。谢谢,我修复了DB.php和Services中$this->conn的问题。phpIt看起来您从未将
$this->conn
设置为任何值。谢谢,我修复了DB.php和Services.php中$this->conn的问题
I got stuck for hours in the code below. I don't know how I can fix this error.
<?php
$Servername = "localhost";
$Username   = "root";
$Password   = "";
$dbName     = "android";
// multi
$conn       = new mysqli($Servername, $Username, $Password, $dbName);
?>
<?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;
   }
  }
?>
<?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)
    // {
    //
    // }


}
?>
<?php require_once '../../startups/DB.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();
 ?>