Call to a member function fetch_assoc() on a non-object

Li1M0ST3Rz

I <3 Bianca
Sep 13, 2010
269
166
PHP:
    if(isset($_GET['login']))
        {
            $error = array();
            if(!isset($_POST['loginusername']) || !isset($_POST['loginpassword']))
                $error[] = 'All the fields must be filled in!';
            if(!count($error))
                {
                    $user = $_POST['loginusername'];
                    $pass = md5(sha1($_POST['loginpassword']));
                    $query = "SELECT * FROM users WHERE username='$user' and password='$pass'";
                    $connect = $conn->query($query);
                    $hit = $connect->num_rows;
                    if($hit)
                        {
                            $take = $hit->fetch_assoc();
                            extract($take);
                            $_SESSION['username'] = $user;
                            header('Location: ./home.php?user');
                        }else{
                           
                            $error[] = 'Wrong username and/or password';
                            header('Location: ./home.php?wrong');
                        }
                       
                }
        }
After i hit login it gives me a fatal error of: Fatal error: Call to a member function fetch_assoc() on a non-object in C:\xampp\htdocs\home\process.php on line 216
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
It's because you're trying to get the array from your row count

$hit is your row count

Change
PHP:
$take = $hit->fetch_assoc();
To
PHP:
$take = $connect->fetch_assoc();

Side note; you may want to think about aptly naming your variables. $connect isn't a suitable name for what it is actually doing.

Another side note: look into escaping/sanitising your inputs. Someone could quite easily hack your website through SQL injections with that code you're using.
 
Last edited:

Users who are viewing this thread

Top