MySQL XAMPP - 致命错误:调用未定义的函数 mysql_connect()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38124614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XAMPP - Fatal error: Call to undefined function mysql_connect()
提问by philip ngu
I downloaded downloading XAMPP for my learning lesson. After I finished the install, I can use it normally. But when I try to connect to MySQL I keep return this error.
我为我的学习课程下载了下载 XAMPP。我安装完成后就可以正常使用了。但是当我尝试连接到 MySQL 时,我不断返回此错误。
<?php
define("DB_HOST","localhost");
define("DB_USER","root");
define("DB_PWD","");
define("DB_DBNAME","text");
define("DB_CHARSET","utf8");
?>
function connect(){
$link=mysql_connect("DB_HOST","DB_USER","DB_PWD") or die("连接失败Error:".mysql_error().":".mysql_error());
mysql_select_db(DB_DBNAME);
return $link;
}
回答by Vivek Pratap Singh
First, Kindly ensure the MySQL service is running.
首先,请确保 MySQL 服务正在运行。
- Open XAMPP Control Panel
- Click on Start button corresponding to MySQL module.
- 打开 XAMPP 控制面板
- 单击 MySQL 模块对应的 Start 按钮。
Then, Try checking to see if the PHP MySQL extension module is being loaded:
然后,尝试检查是否正在加载 PHP MySQL 扩展模块:
<?php
phpinfo();
?>
Run the above code/page and search for mysql. If it's not there, add the following to the php.ini file:
运行上面的代码/页面并搜索mysql。如果不存在,请将以下内容添加到 php.ini 文件中:
extension=php_mysql.dll
Update: mysql_* functions have been removed in PHP 7. You probably have a php7 in XAMPP. Please use PDO or mysqli_connect("DB_HOST","DB_USER","DB_PWD")
instead of mysql_connect().
更新:mysql_* 函数已在 PHP 7 中删除。您可能在 XAMPP 中有一个 php7。请使用 PDO 或mysqli_connect("DB_HOST","DB_USER","DB_PWD")
代替mysql_connect().
回答by ALFIE
First of all looking at your code; you have this part:
首先看你的代码;你有这部分:
function connect(){
$link=mysql_connect("DB_HOST","DB_USER","DB_PWD") or die("连接失败Error:".mysql_error().":".mysql_error());
mysql_select_db(DB_DBNAME);
return $link;
}
After you have closed the php tag("?>"); meaning what comes after the closing php tag is not part of your php code. So try inserting the closing php tag at the end of your code. Something like this:
关闭php标签后("?>"); 这意味着关闭 php 标记之后的内容不是您的 php 代码的一部分。因此,请尝试在代码末尾插入结束 php 标记。像这样的东西:
<?php
define("DB_HOST","localhost");
define("DB_USER","root");
define("DB_PWD","");
define("DB_DBNAME","text");
define("DB_CHARSET","utf8");
function connect(){
$link=mysql_connect("DB_HOST","DB_USER","DB_PWD") or die("连接失败Error:".mysql_error().":".mysql_error());
mysql_select_db(DB_DBNAME);
return $link;
}
?>
As for your code; I would simply rewrite it as:
至于你的代码;我会简单地将其重写为:
<?php
$DB_HOST = "localhost";
$DB_DBNAME = "text";
$DB_USER = "root";
$DB_PWD = "";
$db_link = mysql_connect($DB_HOST, $DB_USER, $DB_PWD) or die('连接失败Error:'.mysql_error());
mysql_select_db($DB_DBNAME, $db_link) or die('连接失败Error:'.mysql_error());
?>
Then try connecting to your MySQL database.
然后尝试连接到您的 MySQL 数据库。