I have just discovered mysql and mysqli, so I’m really a beginner here.
I have a function that I’m using (from an included php file) with all of the variables to connect to the server, OO style. It works like a charm for the home page, but I have a page (using a get request from a form) that tries to use the function and fails. Here is the function:
function connect(){
$servername = "";
$dbusername = "";
$dbpassword = "";
$dbname = "";
$mysqli = new mysqli($servername, $dbusername, $dbpassword, $dbname);
if($mysqli->connect_errno){
echo "Failed to connect to MySQL: (".$mysqli->connect_errno.") ".$mysqli->connect_error;
}
return $mysqli;
}
Is there something about the GET request that’s stopping it? Is this inappropriate practice? I’ve been sorting through the docs, but I’m seriously confused. Here is the code I’m using to call the function:
include_once '/tools/config.php';#This is the file the function is in
#(I even copied the text from the working file)
$mysql = connect();
My debugging amounts to commenting out individual lines until it works. Might there be a better way? Thank you.
There was nothing wrong with the syntax of this function. The problem was in my include statement. Thank you all for your help.