Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mysqli

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?

like image 203
chromedude Avatar asked Sep 17 '11 19:09

chromedude


2 Answers

You need to define: $dbc before

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

ex:

$dbc = mysqli_connect("localhost", "my_user", "my_password", "world");
like image 75
Book Of Zeus Avatar answered Sep 21 '22 11:09

Book Of Zeus


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. :)

like image 35
Ken White Avatar answered Sep 20 '22 11:09

Ken White