我有这个用于 mysql 连接的类,我想在不更改函数名称的情况下将其替换为 mysqli。
class DB { function DB($db_host, $db_user, $db_port, $db_pass, $db_name) {
$link = mysql_connect($db_host, $db_user, $db_pass) or die("Can't connect to database");
mysql_select_db($db_name, $link);
}
public static function execute($sql) {
$result = mysql_query($sql) or die("Could not query:$sql");
return $result;
}
public static function num_rows($result)
{
return mysql_num_rows($result);
}
public static function fetch($result)
{
return mysql_fetch_assoc($result);
}
}
$DB = new DB(DB_HOST, DB_USER, DB_PORT, DB_PASS, DB_NAME);
我试图改变一些东西,但构造函数 $link 有问题(无法执行函数)。致命错误:不在对象上下文中时使用 $this
class DB {
function DB($db_host, $db_user, $db_pass, $db_name) {
$link = mysqli_connect($db_host, $db_user, $db_pass,$db_name) or die(mysqli_connect_error());
return $link;
}
public static function execute($sql) {
//$link = mysqli_connect('localhost', 'root', '' , 'database');
$this->DB();
$result = mysqli_query(**$link**,$sql);
return $result;
}
public static function num_rows($result)
{
return mysqli_num_rows($result);
}
public static function fetch($result)
{
return mysqli_fetch_assoc($result);
}
}
我如何在执行函数中使用构造函数 $link ???有人可以帮助我吗?先感谢您。