gpt4 book ai didi

PHP mysql 错误调用未定义的函数 mysql_connect()

转载 作者:行者123 更新时间:2023-11-29 18:11:18 25 4
gpt4 key购买 nike

我一直在尝试找出问题所在

<?php
require_once("config.php");

class MYSQLDatabase{

private $connection;
// open the connection as soon as object is created
function __construct(){

$this->open_connection();
}

public function open_connection(){

$this->connection = mysql_connect("DB_SERVER", "DB_USER", "DB_PASS");

if(!$this->connection){
die("Database failed " . mysql_error());
}else{
$db_select = mysql_select_db(DB_NAME, $this->connection);
if(!$db_select){
die("Database connection failed " . mysql_error());
}
}
}

public function close_connection(){
if(isset($this->connection)){
mysql_close($this->connection);
unset($this->connection);
}
}

public function query($sql){
$result = mysql_query($sql, $this->connection);
$this->confirm_query($result);
return $result;
}



private function confirm_query($result){
if(!result){
die("Database query failed: " . mysql_error());
}
}
}
$database = new MYSQLDatabase();

?>

当我转到 index.php 文件并使用下面的代码测试该类时,出现以下错误:
该页面无法正常工作

localhost 当前无法处理此请求。HTTP 错误 500。

require_once("../includes/database.php");
if(isset($database)){
echo "true";
}else{
echo "false";
}

最佳答案

正如我刚刚在评论中提到的,mysql 扩展已从 PHP 7 中删除,正如 PHP 手册中所述 mysql is deleted from php 7这就是为什么你收到错误 undefined function mysql

使用mysqli,它是mysql的改进版本。如果您不知道如何使用,这里有一个示例。

<?php
// no need fo mysqli_select_db
$con = mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

有关更多示例,请查看 W3schools

关于PHP mysql 错误调用未定义的函数 mysql_connect(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47380934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com