What does mysqli_error () expect parameter 1 to be mysqli, is null set?

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 returns a Warning: mysqli_error() expects parameter 1 to be mysqli, null given .

Any idea why?

+5
source share
4 answers

You need to determine: $dbc before

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

Example:

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

Book of Zeus has already given you the answer. This is for your future reference (and hopefully helps others in the future).

It is useful to learn to read error messages and try to figure out what might cause them. :)

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

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

A message tells you that the parameter 1 problem was provided by mysqli_error and that it was null when mysqli_error expected it to be mysqli .

So, look at the first parameter that you provide mysqli_error , and you will see its $dbc . Now you know that the problem is that $dbc is null when mysqli_error() called. So see how this $dbc can be null . Oh, right - you didn’t announce it and assign nothing, because the first place it used in the code is here:

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

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

+6
source

undefined variable $dbc in mysqli_error($dbc) . you need to place the obj connection here.

+2
source

Hi, please do not confuse with mysql_connect(); with mysqli_connect();

Try changing as follows

 $conn=mysqli_connect("yourhost", "username", "password", "database-name") or die("initial host/db connection problem");' 

Then, if connected, submit your request using

 mysqli_query($conn, "your query string") or die ("query not executed");' 

Now everything works fine! (mods, removed in quer.ry)

+1
source

All Articles