Here is my DatabaseAccessor class:
<?php
class DatabaseAccessor
{
public $db;
public static function set_db($new_db){
$this->$db = $new_db;
}
public function modifyDatabase($query){
$db->query($query);
}
public function readFromDatabase($query){
$temp = $db->query($query);
return $temp;
}
}
?>
For some reason, it won't work when I try to use it in another file. Here is the file where I'm trying to use it:
<?php
include("/Back-End/DatabaseAccessor.php");
session_start();
$DataBaseAccessor = new DatabaseAccessor();
$temp = new mysqli(getenv('IP'), getenv('C9_USER'), "", "Moneybags", 3306);
$DataBaseAccessor->set_db($temp);
$desiredName = $_POST["username"];
$desiredPassword = $_POST["password"];
$desiredGender = $_POST["gender"];
$query = "INSERT INTO tblUsers (username, password, gender) VALUES ('" . $desiredName . "', '" . $desiredPassword . "', '" . $desiredGender . "')";
try {
$DataBaseAccessor->modifyDatabase($query);
$query = "SELECT * FROM tblUsers WHERE username = '" . $desiredName . "' AND password = '" . $desiredPassword . "'";
$res = $DataBaseAccessor->ReadFromDatabase($query);
$res->data_seek(0);
$row = $res->fetch_assoc();
$_SESSION["LoggedIn"] = true;
$_SESSION["UserID"] = $row['UserID'];
$_SESSION["Username"] = $row['username'];
header("refresh:7;url=index.php");
echo "Account created!";
echo "Returning to main page momentarily...";
}
catch (Exception $e){
echo "Something went wrong with the account creation...";
echo "Returning to main page temporarily...";
header("refresh:7;url=index.php");
}
?>
I tried to pass parameters to the DatabaseAccessor object instead, with a constructor inside to set the mysqli parameters as public properties. That didn't work. I know that my queries work because they were working fine when I was just creating new mysqli objects in every class that needed to access the database.