-1

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

1 Answers1

0

"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)?

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
Vikharry91
  • 32
  • 1