douzhushen_9776 2017-07-21 12:41
浏览 85

致命错误:未捕获错误:在null login.php上调用成员函数query():38 [重复]

This question already has an answer here:

I'm using webstera cpanel for host my web application and here is my code and i'm using session for log in

Fatal error: Uncaught Error: Call to a member function query() on null in /home/buddhika/public_html/login.php:38 Stack trace: #0 {main} thrown in /home/buddhika/public_html/login.php on line 38

<?php
@ob_start();
session_start();
?>

<html>
    <head>
    <?php
include_once('php/db-connect.php');

$error = "";
$msg   = "";

if (isset($_SESSION["isLoggedIn"])) {
    header("Location: index.php");
}

global $sql;

if ($_SERVER['REQUEST_METHOD'] == 'POST') {


    $sql    = "SELECT fname, password FROM login";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while ($row = $result->fetch_assoc()) {
            if (($_POST["username"] == $row["fname"]) && (($_POST["pass"]) == $row["password"])) {
                header("Location: index.php");
                $_SESSION["isLoggedIn"] = true;
                $_SESSION["username"]   = $row["fname"];
                $_SESSION["image"]      = $row["adimage"];

                //alert();
                $msg = "login success.";


            } else {
                $error = "Enter a valid username/password !!!";
            }

        }

    }
}

db connection class

<?php
class Db {
    // The database connection
    protected static $connection;

    /**
     * Connect to the database
     * 
     * @return bool false on failure / mysqli MySQLi object instance on success
     */





    public function connect() {    
        // Try and connect to the database
        if(!isset(self::$connection)) {
            // Load configuration as an array. Use the actual location of your configuration file
            $config = parse_ini_file('./config.ini'); 
            self::$connection = new mysqli('localhost',$config['username'],$config['password'],$config['dbname']);
        }

        // If connection was not successful, handle the error
        if(self::$connection === false) {
            // Handle error - notify administrator, log to a file, show an error screen, etc.
            return false;
        }
        return self::$connection;
    }
public function query($query) {
    // Connect to the database
    $connection = $this -> connect();

    // Query the database
    $result = $connection -> query($query);

    return $result;
}
</div>
  • 写回答

1条回答 默认 最新

  • duanqinqiao4844 2017-07-21 12:54
    关注

    "non-object" means that the variable ($conn) doesn't refer to an object. Did you assign it to your database class (e.g., $conn = new databaseClass();, or whatever the class you're using is named)?

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀