我刚开始做OOP,所以如果有一个简单的解决方案,我提前道歉。基本上我需要在一个类中使用我的$mysqli对象。我把它分成了两个文件。
config2.php
class Config
{
public $host = 'localhost';
public $username = '****';
public $password = '****';
public $database = '****';
function report_error($query)
{
$email = '*@hotmail.com';
$subject = 'MySQL error.';
$message = "IP: {$_SERVER['REMOTE_ADDR']} \n URL: http://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']} \n\n MySQL query: {$query} \n\n MySQL error: " . $mysqli->error();
mail($email, $subject, $message);
die('Oops, an error has occured. The administrator has been notified.');
}
}
$config = new Config();
$mysqli = new mysqli($config->host, $config->username, $config->password, $config->database);
if($mysqli->connect_error)
report_error($mysqli);
管理.php
require('includes/config2.php');
$mysqli->real_escape_string(); // Works out of scope.
class Account
{
public $username = $mysqli->real_escape_string(); // Doesn't work in scope.
public $password;
function login()
{
}
}
感谢您的帮助,我很感激:)。