-1

I'm currently learning php and mysql and am trying to build an authentication webpage where the user registers and is able to log in to a member protected page. The registration process works fine but for some reason I'm getting this error in my login execution script.

Fatal error: Call to a member function prepare() on a non-object in /homepages/8/d459264879/htdocs/tymbi_reg/login_exec.php on line 40

Line 40 is here if($stmt = $mysqli->prepare("SELECT * FROM member WHERE username=? AND password =?"))

I've been trying hard to find where the problem is without any success.

This is the code that I'm using in my login script

<?php

session_start();
require_once('connection.php');
$errmsg_arr = array();
$errflag = false;
$username = $_POST['username'];
$password = $_POST['password'];

if($username == '') {
    $errmsg_arr[] = 'Username missing';
    $errflag = true;
}
if($password == '') {
    $errmsg_arr[] = 'Password missing';
    $errflag = true;
}

if($errflag) {
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    session_write_close();
    header("location: index.php");
    exit();
} else {


    if($stmt = $mysqli->prepare("SELECT * FROM member WHERE username=? AND password =?"))
    {
        $stmt->bind_param("ss", $username, $password);
        $stmt->execute();
        $stmt->store_results();

        if($stmt->num_rows > 0)
        {

            $SESSION['username'] = $username;
            header("Location: home.php");

        }

        else

        {

            $error['alert'] = "Username or password are incorrect";

        }

    }   

}


?>

Here's my connection.php code

<?php
$con=new mysqli("test","test","test","test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

And yes, I have replaced the test values with my details

wannax
  • 77
  • 8

4 Answers4

1

make sure you have $mysqli defined properly

$mysqli = new mysqli('host','user','pass','database_name');
alwaysLearn
  • 6,882
  • 7
  • 39
  • 67
0

You haven't defined $mysqli - your connection. That is why it's not working.

Unless it's defined in connection.php. Could you try var_dump $mysqli and see what you get?

silkfire
  • 24,585
  • 15
  • 82
  • 105
0
$link = mysqli_connect("localhost", "user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

this helps in reading the connection error.

or you can also use

try
{
//your connection
}catch(Exception $e)
{
  echo $e->getMessage();
}
Arun Killu
  • 13,581
  • 5
  • 34
  • 61
-1

I would recommend going all-in on using the Mysqli object and not mixing in the procedural functions. You would have caught your error earlier if you had written the connection logic like so:

$con = new mysqli("host","user","pass","db");

if ($mysqli->errno)
{
    echo "Failed to connect to MySQL: " . $mysqli->error;
}

The OP declared the connection as $con and later tried to access it as $mysqli. mysqli_connect_errno() reports that $con is ok, but $mysqli->errno will fail because $mysqli is not an object at that point, because he named the the connection $con not $mysqli

Dieter Donnert
  • 192
  • 2
  • 9