从 PHP7.4 迁移到 PHP8.0 后数据库连接不工作
Database Connection not working after migrating from PHP7.4 to PHP8.0
也许你能帮我。我尝试从 php7.4 迁移到 php8.0,但我的数据库连接没有正常工作。使用 php7.4 它适用于 php8.0 它不适用。
数据库连接将不再以这种方式建立。
你知道为什么吗?
连接代码:
$db = new sql_db($dbhost, $dbuser, $dbpasswd, $dbname, false);
if(!$db->db_connect_id)
{
message_die(CRITICAL_ERROR, "Could not connect to the database");
}
背后的功能:
function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false)
{
$this->persistency = (version_compare(PHP_VERSION, '5.3.0', '>=')) ? $persistency : false;
$this->user = $sqluser;
$this->password = $sqlpassword;
$this->server = ($this->persistency) ? 'p:' . (($sqlserver) ? $sqlserver : 'localhost') : $sqlserver;
$this->dbname = $database;
$port = (!$port) ? NULL : $port;
$this->db_connect_id = @mysqli_connect($this->server, $this->user, $this->password, $this->dbname, $port);
if( $this->db_connect_id && $database != '')
{
@mysqli_query($this->db_connect_id, "SET NAMES 'ISO-8859-1'");
$this->dbname = $database;
$dbselect = @mysqli_select_db($this->db_connect_id, $this->dbname);
if( $dbselect === false )
{
@mysqli_close($this->db_connect_id);
$this->db_connect_id = $dbselect;
}
return $this->db_connect_id;
}
else
{
return false;
}
}
看起来您一直在依赖古老的、长期弃用的行为,即与 class 同名的方法充当构造函数。这个行为终于被thrown out in PHP 8:
Methods with the same name as the class are no longer interpreted as constructors. The __construct()
method should be used instead.
也许你能帮我。我尝试从 php7.4 迁移到 php8.0,但我的数据库连接没有正常工作。使用 php7.4 它适用于 php8.0 它不适用。
数据库连接将不再以这种方式建立。
你知道为什么吗?
连接代码:
$db = new sql_db($dbhost, $dbuser, $dbpasswd, $dbname, false);
if(!$db->db_connect_id)
{
message_die(CRITICAL_ERROR, "Could not connect to the database");
}
背后的功能:
function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false)
{
$this->persistency = (version_compare(PHP_VERSION, '5.3.0', '>=')) ? $persistency : false;
$this->user = $sqluser;
$this->password = $sqlpassword;
$this->server = ($this->persistency) ? 'p:' . (($sqlserver) ? $sqlserver : 'localhost') : $sqlserver;
$this->dbname = $database;
$port = (!$port) ? NULL : $port;
$this->db_connect_id = @mysqli_connect($this->server, $this->user, $this->password, $this->dbname, $port);
if( $this->db_connect_id && $database != '')
{
@mysqli_query($this->db_connect_id, "SET NAMES 'ISO-8859-1'");
$this->dbname = $database;
$dbselect = @mysqli_select_db($this->db_connect_id, $this->dbname);
if( $dbselect === false )
{
@mysqli_close($this->db_connect_id);
$this->db_connect_id = $dbselect;
}
return $this->db_connect_id;
}
else
{
return false;
}
}
看起来您一直在依赖古老的、长期弃用的行为,即与 class 同名的方法充当构造函数。这个行为终于被thrown out in PHP 8:
Methods with the same name as the class are no longer interpreted as constructors. The
__construct()
method should be used instead.