PHP Warning: mysqli_query() expects parameter 1

I'm looking for assistance in tracking down a PHP warning logged in my error_log file.  All seems correct to me, yet this warning is logged daily.

(The website sells insurance online. The site is in production and sells plenty, but there are tons of little anomalous issues here and there, and I'm trying to track them down by, first, eliminating all obvious errors.)

Below are 4 lines of text in 3 files:

error_log entry:
PHP Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/xxxxxxxx/public_html/_pages/phpAim/lib.inc.php on line 1397

Open in new window


lib.inc.php, line 5:
include_once('db.inc.php');

Open in new window


lib.inc.php, line 1397:
$result = mysqli_query($link, $sql) or die( "Query 1 Error lib.inc.php: " . mysqli_error($link) );

Open in new window


db.inc.php:
<?php 
ini_set('display_errors','0');

$host = "localhost";
$username = "xxxxxxxx";
$password = "xxxxxxxx";
$database = "xxxxxxxx";

$link = mysqli_connect( $host, $username, $password, $database );

if ( mysqli_connect_errno() ){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
?>

Open in new window


Can anyone see a reason for this warning being generated?

Thanks,
Jonathan
Jonathan GreenbergAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leonidas DosasCommented:
Add please and post the following before line 1397:
var_dump($link);
var_dump($sql);

Open in new window

0
gr8gonzoConsultantCommented:
So you have to work backwards. First, the error tells us exactly what the problem is. On this line in lib.inc.php:
$result = mysqli_query($link, $sql) ....

Open in new window

The value of $link is null.

Now, when mysqli_connect() fails, it should return "false", not "null" so chances are that your connection is working, which likely means that $link is being overwritten.

Given the name of that variable, it would not surprise me if you had some other code in your application that used the variable name $link (maybe some code that generates HTML links and uses the variable $link), and under certain circumstances, it leaves the variable as null (or maybe some other value, which might generate a similar but different error).

So my first suggestion would be to grep / search through all of your code for $link and see if that variable is being used by some other code, and being overwritten.

On a side note, this is a good example of how you might want to consider using something like a static class to connect to the database, so that your database connection is wrapped in a nice, global class context. Something like this:

class DB
{
  public static $link;
}

Open in new window


DB::$link = mysqli_connect( $host, $username, $password, $database );

Open in new window


$result = mysqli_query(DB::$link, "query here");

Open in new window


That way, there's very little chance of anyone accidentally overwriting your connection resource variable due to it being a common variable name (and you can also centralize your DB code, too).
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gustavo HernandezceoCommented:
Hi, normally that message is generated due to an error in the query, and the mysql_query is returning no datas.
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

gr8gonzoConsultantCommented:
@Gustavo - no, that error is indicating that parameter 1 to the mysqli_query call, which is $link, is null, so it can't even attempt to run the query. It is like someone telling you to go drive to the store and buy some apples, and that you can use the car in the garage. You say okay, and go to the garage, but there's no car in the garage, so you come back and say, "There's no car!" It's not that the store didn't have apples - you weren't able to even get to the store to check.

The car is like your database connection / link. It lets you drive to the database/store and look for things, but if the link is null, then it's not there.
2
NerdsOfTechTechnology ScientistCommented:
In case this goes to clean-up:
gr8gonzo #a42555574 (best solution, 500 points) *for pinpointing the issue and telling the OP to search for $link being overwritten*
gr8gonzo #a42555627 (assisted solution, 500 points) *amazing clarification and example*

Case in point, the OP might even want to rename $link to another variable like $conn on both lines, to avoid the collision of $link or the hassle of searching for the overwriting $link line within includes, etc.
0
Jonathan GreenbergAuthor Commented:
Thanks, NerdsOfTech. I actually did exactly that - renamed $link to $conn - based upon gr8gonzo's wonderful suggestion that the $link variable may be in use elsewhere - and it was! The only problem was that, after changing $link to $conn in a million places, I then discovered that $conn is in use elsewhere in the app. But this is in only one instance, and it appears safe to simply comment out the one instance, although I'll likely need to go back in and update my variable again.

I'm going to close this up just as you've suggested.

One last comment is to gr8gonzo: I'm very interested in your suggestion about wrapping my db connection "in a nice, global class context," but I'm unfamiliar with PHP classes. If you have a simple tutorial to refer me to, that would be much appreciated.

Thanks to all who participated!

Regards,
Jonathan
0
Jonathan GreenbergAuthor Commented:
gr8gonzo: thank you again for the fantastic solution and explanation!
0
gr8gonzoConsultantCommented:
Sure thing. I've written 2 articles on it:

https://www.experts-exchange.com/articles/2626/Beginning-Object-Oriented-Programming-in-PHP.html

https://www.experts-exchange.com/articles/2631/Advanced-Object-Oriented-Programming-in-PHP.html

Static classes are mentioned near the end of the second article, but you should read through them both to better understand the concepts.
1
gr8gonzoConsultantCommented:
Oh, and I don't think I mention this in the article, but one other advantage of the static class is that you don't have to use "global" to bring it into the scope of a function. For example, if you had this code:

function doSomething()
{
  global $conn;
  mysqli_query($conn, "blah blah");
}

By using the static class property, you could just use the property directly - no "global" calls to bring the variable into the function scope.

function doSomething()
{
  mysqli_query(DB::$conn, "blah blah");
}
1
Jonathan GreenbergAuthor Commented:
Thanks again, gr8gonzo, very generous of you!

Regards,
Jonathan
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.