I've seen a few people with this same issue, yet none of the answers solved my problem. This is what I have in three files:
index.php:
$dbconnect = connectDB("mydb","connect");
mysqli_query($dbconnect,"INSERT INTO `mytable` (field1,field2) values ('value 1','value 2')");
connectDB("","kill",$dbconnect);
functions.php
function connectDB($db_name,$connectorkill,$link) { include('connectDB.inc.php'); }
connectDB.inc.php
$host="localhost";
$user="myusername";
$pass="mypassword";
if($connectorkill == "connect") {
if($dblink = mysqli_connect($host, $user, $pass, $db_name)) {
echo "dblink created.";
return $dblink;
} else { echo "Error connecting to database."; }
} elseif($connectorkill == "kill") { mysqli_close($link); }
and what I get is:
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/username/public_html/members/index.php on line 2
which looks like the connectDB function is not doing its job, specifically the return of the $dblink variable in connectDB.inc.php. I went ahead and moved the code from connectDB.inc.php to inside the functions.php and it solved the problem, but I don't want it set up that way. Is there a way to solve this error?