Plamen Dobrev
Plamen Dobrev

Reputation: 91

Notice: Trying to access array offset on value of type bool in line XX

Trying to access array offset on value of type bool in line 88

Trying to access array offset on value of type bool in line 91

I marked the lines down below, I tried to change the query just in case but it doesn't seem to be working. I looked it up and I just couldn't find a solution. I also noticed all these answers were downvoted but I am a newbie in PHP, so I don't really know what to look for. I suppose that's something simple but I'm just not looking at the right place. Why is that happening? And how can I fix it?

$number = rand(pow(10, 11-1), pow(10, 11)-1);
$stmt = mysqli_prepare($db, "SELECT * FROM accounts WHERE email= ? OR number= ? LIMIT 1");
mysqli_stmt_bind_param($stmt, "ss", $email, $number);
mysqli_stmt_execute($stmt);

$user = mysqli_stmt_fetch($stmt);

function uniqueNum() {
    global $number;
    $number = rand(pow(10, 11-1), pow(10, 11)-1);
    if ($user['number'] === $number) {
        uniqueNum();
    }
}

if ($user) {
    if ($user['email'] === $email) { // Line 88
        array_push($errors, "E-Mail already exists");
    }
    if ($user['number'] === $number) { // Line 91
        uniqueNum();
    }
}

Any kind of help is appreciated! I'm 15 and I'm just learning how to do registration forms, so I would happily listen to some extra advice!

Upvotes: 1

Views: 1207

Answers (1)

Dharman
Dharman

Reputation: 33375

You are confusing PDO with mysqli. Mysqli is much more complicated and mysqli_stmt_fetch does not actually return any values. You can make this much simpler if you use get_result and OOP-style. You can then use for example, fetch_assoc() to get a single row out of the result object.

$stmt = $db->prepare("SELECT * FROM accounts WHERE email= ? OR number= ? LIMIT 1");
$stmt->bind_param("ss", $email, $number);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();

if($user) {
    if ($user['email'] === $email) {
        array_push($errors, "E-Mail already exists");
    }
    if ($user['number'] === $number) {
        uniqueNum();
    }
}

Upvotes: 2

Related Questions