致命错误:未捕获错误:调用未定义函数mysql_connect()

时间:2022-01-03 07:00:14

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
Stack trace: #0 {main} thrown in C:\xampp\htdocs\register.php on line 22

致命错误:未捕获错误:在C:\xampp\htdoc \寄存器中调用未定义函数mysql_connect()。php:22堆栈跟踪:#0 {main}抛出C:\xampp\htdocs\寄存器。php在第22行

Example of line 22:

第22行的例子:

$link = mysql_connect($mysql_hostname , $mysql_username);

7 个解决方案

#1


75  

mysql_* functions have been removed in PHP 7.

在PHP 7中已经删除了mysql_*函数。

You probably have PHP 7 in XAMPP. You now have two alternatives: MySQLi and PDO.

XAMPP中可能有PHP 7。您现在有两种选择:MySQLi和PDO。

Additionally, here is a nice wiki page about PDO.

此外,这里有一个关于PDO的很好的wiki页面。

#2


23  

You can use mysqli_connect($mysql_hostname , $mysql_username) instead of mysql_connect($mysql_hostname , $mysql_username).

您可以使用$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。

#3


14  

It is recommended to use either the MySQLi or PDO extensions. 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.

建议使用MySQLi或PDO扩展。不建议在新开发中使用旧的mysql扩展,因为在PHP 5.5.0中已经废弃,在PHP 7中也已经删除。

PHP offers three different APIs to connect to MySQL. Below we show the APIs provided by the mysql, mysqli, and PDO extensions. Each code snippet creates a connection to a MySQL server running on "example.com" using the username "username" and the password "password". And a query is run to greet the user.

PHP提供了三种不同的api来连接到MySQL。下面我们将展示mysql、sqmyli和PDO扩展提供的api。每个代码段使用用户名“username”和密码“password”创建一个连接到“example.com”上运行的MySQL服务器。并运行查询来问候用户。

Example #1 Comparing the three MySQL APIs

示例#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_*函数?

#4


2  

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 can't upgrade your project overnight you can

如果不能在一夜之间升级你的项目,你可以

downgrade your version of php to whatever version that worked

将php的版本降级为任何有效的版本

or...

还是……

use a shim (kinda polyfill) such as this https://github.com/dshafik/php7-mysql-shim or https://github.com/dotpointer/mysql-shim then find a place for include_once("choice_shim.php"); somewhere on your code

使用类型为https://github.com/dshafik/php7-mysql-shim或https://github.com/dotpointer/mysql-shim的类型(“choice_shim.php”);在你的代码

That will keep your old php code a up and running until you are in a mood to update...

这将使您的旧php代码继续运行,直到您有心情更新……

#5


1  

mysql_ functions have been removed from PHP 7. You can now use MySQLi or PDO.

mysql_函数已经从PHP 7中删除。现在可以使用MySQLi或PDO。

MySQLi example:

MySQLi例子:

mysqli_connect($mysql_hostname, $mysql_username, $mysql_password, $mysql_dbname);

mysqli_connect reference link

mysqli_connect参考链接

#6


-1  

You got that error because the mysql_connect function (actually, all mysql_* functions) were removed from PHP 7. You can now use MySQLi or PDO.

您得到这个错误是因为从PHP 7中删除了mysql_connect函数(实际上,所有mysql_*函数)。现在可以使用MySQLi或PDO。

Example:

例子:

$mysqli = new mysqli($hostname, $username, $password, $database);

#7


-2  

mysql_* functions have been removed in PHP 7.

在PHP 7中已经删除了mysql_*函数。

You now have two alternatives: MySQLi and PDO.

您现在有两种选择:MySQLi和PDO。

The following is a before (-) and after (+) comparison of some common changes to MySQLi, 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)) {

-while ($row = mysql_fetch_array( $result, MYSQL_ASSOC )) {
+while ($row = mysqli_fetch_array( $result, MYSQLI_ASSOC )) {

-mysql_close($dbLink);
+mysqli_close($dbLink);

#1


75  

mysql_* functions have been removed in PHP 7.

在PHP 7中已经删除了mysql_*函数。

You probably have PHP 7 in XAMPP. You now have two alternatives: MySQLi and PDO.

XAMPP中可能有PHP 7。您现在有两种选择:MySQLi和PDO。

Additionally, here is a nice wiki page about PDO.

此外,这里有一个关于PDO的很好的wiki页面。

#2


23  

You can use mysqli_connect($mysql_hostname , $mysql_username) instead of mysql_connect($mysql_hostname , $mysql_username).

您可以使用$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。

#3


14  

It is recommended to use either the MySQLi or PDO extensions. 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.

建议使用MySQLi或PDO扩展。不建议在新开发中使用旧的mysql扩展,因为在PHP 5.5.0中已经废弃,在PHP 7中也已经删除。

PHP offers three different APIs to connect to MySQL. Below we show the APIs provided by the mysql, mysqli, and PDO extensions. Each code snippet creates a connection to a MySQL server running on "example.com" using the username "username" and the password "password". And a query is run to greet the user.

PHP提供了三种不同的api来连接到MySQL。下面我们将展示mysql、sqmyli和PDO扩展提供的api。每个代码段使用用户名“username”和密码“password”创建一个连接到“example.com”上运行的MySQL服务器。并运行查询来问候用户。

Example #1 Comparing the three MySQL APIs

示例#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_*函数?

#4


2  

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 can't upgrade your project overnight you can

如果不能在一夜之间升级你的项目,你可以

downgrade your version of php to whatever version that worked

将php的版本降级为任何有效的版本

or...

还是……

use a shim (kinda polyfill) such as this https://github.com/dshafik/php7-mysql-shim or https://github.com/dotpointer/mysql-shim then find a place for include_once("choice_shim.php"); somewhere on your code

使用类型为https://github.com/dshafik/php7-mysql-shim或https://github.com/dotpointer/mysql-shim的类型(“choice_shim.php”);在你的代码

That will keep your old php code a up and running until you are in a mood to update...

这将使您的旧php代码继续运行,直到您有心情更新……

#5


1  

mysql_ functions have been removed from PHP 7. You can now use MySQLi or PDO.

mysql_函数已经从PHP 7中删除。现在可以使用MySQLi或PDO。

MySQLi example:

MySQLi例子:

mysqli_connect($mysql_hostname, $mysql_username, $mysql_password, $mysql_dbname);

mysqli_connect reference link

mysqli_connect参考链接

#6


-1  

You got that error because the mysql_connect function (actually, all mysql_* functions) were removed from PHP 7. You can now use MySQLi or PDO.

您得到这个错误是因为从PHP 7中删除了mysql_connect函数(实际上,所有mysql_*函数)。现在可以使用MySQLi或PDO。

Example:

例子:

$mysqli = new mysqli($hostname, $username, $password, $database);

#7


-2  

mysql_* functions have been removed in PHP 7.

在PHP 7中已经删除了mysql_*函数。

You now have two alternatives: MySQLi and PDO.

您现在有两种选择:MySQLi和PDO。

The following is a before (-) and after (+) comparison of some common changes to MySQLi, 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)) {

-while ($row = mysql_fetch_array( $result, MYSQL_ASSOC )) {
+while ($row = mysqli_fetch_array( $result, MYSQLI_ASSOC )) {

-mysql_close($dbLink);
+mysqli_close($dbLink);