What does the mysqli_error() expects parameter 1 to be mysqli, null given mean?

I have this PHP page:

<?php

//$_GET['invite'] = kNdqyJTjcf;

$code = mysqli_real_escape_string ($dbc, $_GET['invite']);

$q = "SELECT invite_id FROM signups_invited WHERE (code = '$code') LIMIT 1";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

if (mysqli_num_rows($r) == 1) {
    echo 'Verified';
} else {
    echo 'That is not valid. Sorry.';
}

?>

This is returning the error Warning: mysqli_error() expects parameter 1 to be mysqli, null given.

Any idea why?


Solution 1:

You need to define: $dbc before

 $code = mysqli_real_escape_string ($dbc, $_GET['invite']);

ex:

$dbc = mysqli_connect("localhost", "my_user", "my_password", "world");

Solution 2:

It's helpful to learn to read error messages and try to figure out what might be causing them. :)

This one tells you exactly what the problem is, and where to start looking.

Warning: mysqli_error() expects parameter 1 to be mysqli, null given.

The message tells you that the problem is parameter 1 provided to mysqli_error, and that it was null when mysqli_error expected it to be a mysqli.

So look at the first parameter you're providing to mysqli_error, and you'll see it's $dbc. You know now that the problem is that $dbc is null when the call to mysqli_error() is made. So look at how it is that $dbc can be null. Oh, right - you didn't declare it and assign anything to it, because the first place it's used in the code is here:

$code = mysqli_real_escape_string ($dbc, $_GET['invite']);

and it's being used as if it's already something other than null. Since this is at the start of your code, the problem is that you forgot to declare and initialize it by connecting to the database. Problem solved. :)

Solution 3:

undefined variable $dbc in mysqli_error($dbc). you need to put your connection obj here.