[英]Fatal error: Uncaught Error: Call to undefined function mysql_connect()
I am trying to do a simple connection with XAMPP and MySQL server, but whenever I try to enter data or connect to the database, I get this error.我正在尝试与 XAMPP 和 MySQL 服务器进行简单连接,但每当我尝试输入数据或连接到数据库时,都会出现此错误。
Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\xampp\htdocs\register.php:22致命错误:未捕获错误:调用未定义的 function mysql_connect() in C:\xampp\htdocs\register.php:22
Stack trace: #0 {main} thrown in C:\xampp\htdocs\register.php on line 22堆栈跟踪:#0 {main} 在第 22 行的 C:\xampp\htdocs\register.php 中抛出
Example of line 22:第 22 行示例:
$link = mysql_connect($mysql_hostname , $mysql_username);
You can use mysqli_connect($mysql_hostname , $mysql_username)
instead of mysql_connect($mysql_hostname , $mysql_username)
.您可以使用mysqli_connect($mysql_hostname , $mysql_username)
代替mysql_connect($mysql_hostname , $mysql_username)
。
mysql_*
functions were removed as of PHP 7. You now have two alternatives: MySQLi and PDO .自 PHP 7 起, mysql_*
函数已被删除。您现在有两个选择: MySQLi和PDO 。
It is recommended to use either the MySQLi or PDO extensions.建议使用 MySQLi 或 PDO 扩展。 It is not recommended to use the old mysql extension for new development, as it was deprecated in PHP 5.5.0 and was removed in PHP 7.不建议在新开发中使用旧的 mysql 扩展,因为它在 PHP 5.5.0 中已被弃用,并在 PHP 7 中被删除。
PHP offers three different APIs to connect to MySQL. PHP 提供了三种不同的 API 来连接 MySQL。 Below we show the APIs provided by the mysql, mysqli, and PDO extensions.下面我们展示 mysql、mysqli 和 PDO 扩展提供的 API。 Each code snippet creates a connection to a MySQL server running on "example.com" using the username "username" and the password "password".每个代码片段使用用户名“username”和密码“password”创建与“example.com”上运行的 MySQL 服务器的连接。 And a query is run to greet the user.并运行一个查询来问候用户。
Example #1 Comparing the three MySQL APIs Example #1 比较三个 MySQL API
<?php
// mysqli
$mysqli = new mysqli("example.com", "username", "password", "database");
$result = $mysqli->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = $result->fetch_assoc();
echo htmlentities($row['_message']);
// PDO
$pdo = new PDO('mysql:host=example.com;dbname=database', 'username', 'password');
$statement = $pdo->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = $statement->fetch(PDO::FETCH_ASSOC);
echo htmlentities($row['_message']);
// mysql
$c = mysql_connect("example.com", "username", "password");
mysql_select_db("database");
$result = mysql_query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = mysql_fetch_assoc($result);
echo htmlentities($row['_message']);
?>
I suggest you try out both MySQLi and PDO and find out what API design you prefer.我建议您同时尝试 MySQLi 和 PDO 并找出您喜欢的 API 设计。
Read Choosing an API and Why shouldn't I use mysql_* functions in PHP?阅读选择 API和为什么我不应该在 PHP 中使用 mysql_* 函数?
As other answers suggest... Some guy (for whatever reason) decided that your old code should not work when you upgrade your PHP, because he knows better than you and don't care about what your code does or how simple it is for you to upgrade.正如其他答案所暗示的那样......有些人(无论出于何种原因)认为您的旧代码在您升级 PHP 时不应该工作,因为他比您更了解并且不关心您的代码做什么或它有多简单你来升级。
Well, if you can't upgrade your project overnight you can好吧,如果你不能在一夜之间升级你的项目,你可以
downgrade your version of PHP to whatever version that worked将您的 PHP 版本降级到任何可用的版本
or...或者...
use a shim (kind of polyfill) such as https://github.com/dshafik/php7-mysql-shim or https://github.com/dotpointer/mysql-shim , and then find a place for
include_once("choice_shim.php");
使用 shim(一种 polyfill),例如https://github.com/dshafik/php7-mysql-shim或https://github.com/dotpointer/mysql-shim ,然后为include_once("choice_shim.php");
somewhere in your code在您的代码中的某处
That will keep your old PHP code up and running until you are in a mood to update...这将使您的旧 PHP 代码保持正常运行,直到您有心情更新...
mysql_*
functions have been removed in PHP 7. mysql_*
函数已在 PHP 7 中删除。
You now have two alternatives: MySQLi and PDO .您现在有两个选择: MySQLi和PDO 。
The following is a before (-) and after (+) comparison of a migration to the MySQLi alternative, taken straight out of working code:以下是迁移到 MySQLi 替代方案之前 (-) 和之后 (+) 的比较,直接取自工作代码:
-if (!$dbLink = mysql_connect($dbHost, $dbUser, $dbPass))
+if (!$dbLink = mysqli_connect($dbHost, $dbUser, $dbPass))
-if (!mysql_select_db($dbName, $dbLink))
+if (!mysqli_select_db($dbLink, $dbName))
-if (!$result = mysql_query($query, $dbLink)) {
+if (!$result = mysqli_query($dbLink, $query)) {
-if (mysql_num_rows($result) > 0) {
+if (mysqli_num_rows($result) > 0) {
-while ($row = mysql_fetch_array( $result, MYSQL_ASSOC )) {
+while ($row = mysqli_fetch_array( $result, MYSQLI_ASSOC )) {
-mysql_close($dbLink);
+mysqli_close($dbLink);
Make sure you have not committed a typo as in my case确保您没有像我一样犯错
msyql_fetch_assoc should be mysql msyql_fetch_assoc 应该是mysql
Reason behind Fatal error such as :致命错误背后的原因,例如:
are mainly caused due to upgrading to the upper version of PHP (>=7).主要是升级到PHP高版本(>=7)造成的。
In such a case better to modify your code and use PDO or Mysql improved version with the latest PHP version so that you can have several benefits likes在这种情况下,最好修改您的代码并使用 PDO 或 Mysql 改进版本和最新的 PHP 版本,这样您可以获得一些好处,例如
You may visit here<\/a> to check for PDO\/Mysqli connections and query examples.您可以访问此处<\/a>以检查 PDO\/Mysqli 连接和查询示例。
"For mysqli you can use :对于 mysqli,您可以使用:
$db = ADONewConnection('mysqli'); $db = ADONewConnection('mysqli');
... ... ……
$db->execute("set names 'utf8'"); $db->execute("设置名称 'utf8'");
in case of a similar issue when I'm creating dockerfile I faced the same scenario:- I used below changed in mysql_connect function as:-如果在创建 dockerfile 时遇到类似问题,我会遇到相同的情况:- 我在 mysql_connect 函数中使用以下更改为:-
if($CONN = @mysqli_connect($DBHOST, $DBUSER, $DBPASS)){ //mysql_query("SET CHARACTER SET 'gbk'", $CONN); if($CONN = @mysqli_connect($DBHOST, $DBUSER, $DBPASS)){ //mysql_query("SET CHARACTER SET 'gbk'", $CONN);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.