Class 无法从其他类获取mysqli标识符
我有两个类:一个用于sql,另一个用于身份验证。这些是单独的文件,只能由自动加载程序在另一个文件index.php中组合在一起。sql类如下所示:Class 无法从其他类获取mysqli标识符,class,mysqli,identifier,Class,Mysqli,Identifier,我有两个类:一个用于sql,另一个用于身份验证。这些是单独的文件,只能由自动加载程序在另一个文件index.php中组合在一起。sql类如下所示: class aF_sql { public static $open; public static $char; public static $close; public static function open() { global $config; self::$open = m
class aF_sql {
public static $open;
public static $char;
public static $close;
public static function open() {
global $config;
self::$open = mysqli_connect($config['aF_sql_host'], $config['aF_sql_user'], $config['aF_sql_pass'], $config['aF_sql_data']);
if(!self::$open) {
die('<b>ERROR (/core/sql/1): Could not initialize database connection</b>');
}
self::$char = mysqli_set_charset(self::$open, $config['aF_sql_char']);
if(!self::$char) {
die('<br>ERROR (/core/sql/2): Could not set database charset</br>');
}
return TRUE;
}
public static function close() {
self::$close = mysqli_close(self::$open);
if(!self::$close) {
die('<b>ERROR (/core/sql/3): Could not close database connection');
}
return TRUE;
}
}
错误是这样说的:警告:mysqli\u real\u escape\u字符串期望参数1为mysqli,如果给定空值。。。我应该使用什么作为能够执行查询的标识符?正如您所知,mysqli需要连接标识符
非常感谢好的,我想出来了!我没有在类中返回TRUE,而是返回self::$open。这样,我就可以在软件中的任何地方使用aF_sql::$open,它引用连接标识符。换句话说,它不起作用,因为我没有在类中返回变量
class aF_sql {
public static $open;
public static $char;
public static $close;
public static function open() {
global $config;
self::$open = mysqli_connect($config['aF_sql_host'], $config['aF_sql_user'], $config['aF_sql_pass'], $config['aF_sql_data']);
if(!self::$open) {
die('<b>ERROR (/core/sql/1): Could not initialize database connection</b>');
}
self::$char = mysqli_set_charset(self::$open, $config['aF_sql_char']);
if(!self::$char) {
die('<br>ERROR (/core/sql/2): Could not set database charset</br>');
}
return self::$open;
}
public static function close() {
self::$close = mysqli_close(self::$open);
if(!self::$close) {
die('<b>ERROR (/core/sql/3): Could not close database connection');
}
return TRUE;
}
mysqli_real_escape_string(aF_sql::$open, $_POST['username']);
就这样
class aF_sql {
public static $open;
public static $char;
public static $close;
public static function open() {
global $config;
self::$open = mysqli_connect($config['aF_sql_host'], $config['aF_sql_user'], $config['aF_sql_pass'], $config['aF_sql_data']);
if(!self::$open) {
die('<b>ERROR (/core/sql/1): Could not initialize database connection</b>');
}
self::$char = mysqli_set_charset(self::$open, $config['aF_sql_char']);
if(!self::$char) {
die('<br>ERROR (/core/sql/2): Could not set database charset</br>');
}
return self::$open;
}
public static function close() {
self::$close = mysqli_close(self::$open);
if(!self::$close) {
die('<b>ERROR (/core/sql/3): Could not close database connection');
}
return TRUE;
}
mysqli_real_escape_string(aF_sql::$open, $_POST['username']);