This seems extremely easy but for the life of me can't figure out why this is not working. I am trying to simply pass an object into a function but I get this error:
Catchable fatal error: Argument 3 passed to write_query() must be an instance of mysqli, null given, called in C:\xampp\htdocs\CSP\login\login.php on line 28 and defined in C:\xampp\htdocs\CSP\php_scripts\login_script.php on line 40
These are my functions definitions:
function conn_database()
{
if (!empty($_POST['userid']) && !empty($_POST['password']))
{
$conn = new mysqli('localhost', 'root', '', 'csp');
if (mysqli_connect_errno())
{
echo 'Connection to database failed:'.mysqli_connect_error();
exit();
}
return $conn;
}
}
function write_query($userid, $password, mysqli $conn)
{
$query = 'select * from authorized_users '
."where userid= '$userid'"
."and password_id='$password'";
$result = $conn->query($query);
return $result;
}
I am calling them like this:
$conn = conn_database();
$result = write_query($userid, $password, $conn);
I'm type hinting in the definition of the write_query() but from what I understand from the error, my third argument is not considered an instance of mysqli. (I am aware I should be salting the password and all, I'm just trying to get this to work first). Any suggestions would be appreciated