1

I keep getting this error. 'Notice: Trying to get property of non-object in C:\xampp\htdocs\Homework\Sessions\Exercises\Includes\Login.php on line 21'
Here is my code below. I'm trying to connect and log into a mysql database but I keep getting thesame error.

Any ideas? Thanks

<?php
    $dbEntries = $_POST;
    foreach ($dbEntries as &$entry) {
        $entry = dbString($entry);
    }

    @$db = new mysqli('mysql13.ezhostingserver.com', 'webdata', 'W3bD@t@', 'allicense');

    if (mysqli_connect_errno()) {
        echo 'Cannot connect to database: ' . mysqli_connect_error();
    } else {
        $query = "SELECT id AS EmployeeID, first_name AS FirstName, last_name AS LastName
                    FROM northwind_employees
                    WHERE email_address = '" . $dbEntries['Email'] .
                "' AND password = '" . $dbEntries['Password'] . "'";
        $result = $db->query($query);

        if ($result->num_rows) {
            $row = $result->fetch_assoc();
            $msg = 'Logged in as ' . $row['FirstName'] . ' ' . $row['LastName'];
            $_SESSION['FirstName'] = $row['FirstName'];
            $_SESSION['LastName'] = $row['LastName'];
            $_SESSION['EmployeeID'] = $row['EmployeeID'];
            if (isset($_POST['Remember'])) {
                setcookie('eid', $row['EmployeeID'], time() + 60 * 60 * 24 * 7);
            }
        } else {
            $msg = 'Login Failed';
            unset($_POST['LoggingIn']);
            $dbEntries = $_POST;
        }
    }
?>
mitkosoft
  • 5,262
  • 1
  • 13
  • 31
Becca
  • 169
  • 1
  • 4
  • 12
  • 2
    You should indicate which is line 21 in the code sample. Its probably this though `@$db` should be just `$db` – Mike Miller Apr 28 '16 at 08:21
  • Possible duplicate of [Trying to get property of non-object in](http://stackoverflow.com/questions/5891911/trying-to-get-property-of-non-object-in) – Héctor Valverde Apr 28 '16 at 08:21
  • @$db = new mysqli('mysql13.ezhostingserver.com', 'webdata', 'W3bD@t@', 'allicense'); - the @ is suppressing errors. So remove that, and you should be able to find out what the issue is on that line – AceWebDesign Apr 28 '16 at 08:24
  • Okay Thanks. I figured it out.. :) – Becca Apr 28 '16 at 08:40

0 Answers0