just building a simple connection to an SQL DB.
I wrote thise simple connection that is function invoked, all of the PHP core API functions are mysqli
and not mysql
so no mixing is going on, maybe I missed something rather basic here?
The error i'm receiving is: Warning: mysqli_query() expects parameter 1 to be mysqli, null given in G:\xampp\htdocs\phpBackToBasics\connect.php on line 29
I put a vardump to check $connection
and it's returned true by mysqli_connect
.
connection.php
function db_connection()
{
static $connection;
if (isset($connection)) {
$host = 'localhost';
$config = parse_ini_file('config.ini');
$connection = mysqli_connect($host, $config['username'], $config['password'], $config['phpBackToBasics']);
}
if ($connection === false) {
return mysqli_connect_error();
}
return $connection;
}
function db_query($query)
{
$connection = db_connection();
var_dump(print_r($connection)); //<== true.
$result = mysqli_query($connection, $query);
if ($result === false) {
return mysqli_error($connection);
}
return $result;
}
function db_error()
{
$connection = db_connection();
return mysqli_error($connection);
}
create.php
include_once 'connect.php';
$result = db_query("INSERT INTO 'users' ('name', 'email') VALUES ('boolBool', 'bool@bool.com')");
if ($result === false) {
$error = db_error();
echo 'you have an error: '.$error;
} else {
return 'Row has been added';
}