Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Call to a member function fetch_assoc() on boolean ERROR

Hi, I am transfering my projekt from my slow windows computer to my macbook. In the process I have been encountered with this error Call to a member function fetch_assoc() on boolean I had to rewrite the SQL code for the new databases btw.

Here is my login.php script. The error is in the if statements condition.

<?php

session_start();

include "dbh.php";

$uid = $_POST['uid'];
$pwd = $_POST['pwd'];

$sql = "SELECT * FROM user WHERE uid = '$uid' AND pwd = '$pwd'";
$result = $conn->query($sql);

if (!$row = $result->fetch_assoc()) {
    echo "Your username or password is incorrect";
} else {
    $_SESSION['id'] = $row['id'];
    $_SESSION['uid'] = $row['uid'];
    $_SESSION['is_teacher'] = $row['is_teacher'];
}

header('Location: index.php');

I thought it might have something to do with the database handler or the connection as well so here it is.

dbh.php script

<?php

$conn = mysqli_connect("localhost", "root", "", "logintest");

if (!$conn) {
    die("Connection failed: ".mysqli_connect_error());
}