[英]PHP mysql error Call to undefined function mysql_connect()
我一直在尝试找出问题所在
<?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文件并使用下面的代码测试该类时,出现以下错误:
此页面无效
本地主机当前无法处理此请求。 HTTP错误500。
require_once("../includes/database.php");
if(isset($database)){
echo "true";
}else{
echo "false";
}
正如我刚才在评论中提到的那样,从PHP 7中deleted
mysql
扩展,因为PHP手册说这里是从php 7中删除了mysql,所以这就是为什么您会得到错误的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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.