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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34579099/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:05:37  来源:igfitidea点击:

Fatal error: Uncaught Error: Call to undefined function mysql_connect()

phpmysqlxampp

提问by Demeteor

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\htdocs\register.php:22 中未定义的函数 mysql_connect()
堆栈跟踪:#0 {main} 在第 22 行的 C:\xampp\htdocs\register.php 中抛出

Example of line 22:

第 22 行的示例:

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

回答by efik

mysql_*functions have been removed in PHP 7.

mysql_*PHP 7 中删除了函数。

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

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

Additionally, hereis a nice wiki page about PDO.

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

回答by user6128099

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: MySQLiand PDO.

mysql_*自 PHP 7 起删除了函数。您现在有两种选择:MySQLiPDO

回答by Abhijit Jagtap

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、mysqli 和 PDO 扩展提供的 API。每个代码片段都使用用户名“username”和密码“password”创建到在“example.com”上运行的 MySQL 服务器的连接。并运行一个查询来迎接用户。

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 APIand Why shouldn't I use mysql_* functions in PHP?

阅读选择 API为什么我不应该在 PHP 中使用 mysql_* 函数?

回答by konzo

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-shimor https://github.com/dotpointer/mysql-shim, and then find a place for include_once("choice_shim.php");somewhere in your code

使用垫片(一种 polyfill),例如https://github.com/dshafik/php7-mysql-shimhttps://github.com/dotpointer/mysql-shim,然后include_once("choice_shim.php");在代码中找到一个位置

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

这将使您的旧 PHP 代码保持正常运行,直到您有心情更新...

回答by user664833

mysql_*functions have been removed in PHP 7.

mysql_*PHP 7 中删除了函数。

You now have two alternatives: MySQLiand PDO.

您现在有两种选择:MySQLiPDO

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);

回答by Dhaval Ajani

mysql_functions have been removed from PHP 7. You can now use MySQLior PDO.

mysql_函数已从 PHP 7 中删除。您现在可以使用MySQLiPDO

MySQLi example:

MySQLi 示例:

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

mysqli_connectreference link

mysqli_connect参考链接

回答by rashedcs

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

您收到该错误是因为该mysql_connect函数(实际上是所有mysql_*函数)已从 PHP 7 中删除。您现在可以使用MySQLiPDO

Example:

例子:

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

回答by Hammad Khan

Make sure you have not committed a typo as in my case

确保你没有像我的情况那样犯错

msyql_fetch_assoc should be mysql

msyql_fetch_assoc 应该是mysql

回答by desbest

There's a way you can use mysql_connectand all mysql_functions in PHP7 without you having to convert all your mysql_functions to mysqli_by using the free mysql to mysqli class.

有一种方法可以使用PHP7 中的mysql_connect所有mysql_函数,而无需使用免费的 mysql to mysqli 类将所有mysql_函数转换mysqli_

回答by user1077915

For mysqli you can use :

对于 mysqli,您可以使用:

$db = ADONewConnection('mysqli');

$db = ADONewConnection('mysqli');

... ...

……

$db->execute("set names 'utf8'");

$db->e​​xecute("设置名称'utf8'");