这是我的DatabaseAccessor类:
<?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;
}
}
?>
出于某种原因,当我尝试在另一个文件中使用它时,它不会工作。这是我尝试使用它的文件:
<?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");
}
?>
我尝试将参数传递给DatabaseAccessor对象,并在其中使用构造函数将mysqli参数设置为公共属性。那不行。我知道我的查询有效,因为当我在每个需要访问数据库的类中创建新的mysqli对象时,它们工作正常。