简体   繁体   English

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

[英]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);

mysql_* functions have been removed in PHP 7. mysql_*函数已在 PHP 7 中删除。

You probably have PHP 7 in XAMPP.您可能在 XAMPP 中有 PHP 7。 You now have two alternatives: MySQLi and PDO .您现在有两个选择: MySQLiPDO

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_*函数已被删除。您现在有两个选择: MySQLiPDO

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-shimhttps://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 .您现在有两个选择: 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);

mysql_ functions have been removed from PHP 7. You can now use MySQLi or PDO . mysql_函数已从 PHP 7 中删除。您现在可以使用MySQLiPDO

MySQLi example: MySQLi 示例:

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

mysqli_connect reference link mysqli_connect参考链接

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_*函数)。您现在可以使用MySQLiPDO了

Example: 例:

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

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 :致命错误背后的原因,例如:

  • undefined function mysql_connect未定义函数 mysql_connect<\/li>
  • undefined function mysql_fetch_array未定义函数 mysql_fetch_array<\/li>
  • undefined function mysql_select_db未定义的函数 mysql_select_db<\/li>
  • undefined function mysql_*未定义函数 mysql_*<\/li><\/ul>

    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 版本,这样您可以获得一些好处,例如

For mysqli you can use :对于 mysqli,您可以使用:

$db = ADONewConnection('mysqli'); $db = ADONewConnection('mysqli');

... ... ……

$db->execute("set names 'utf8'"); $db->e​​xecute("设置名称 '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.

相关问题 致命错误:未捕获错误:调用未定义函数mysql_connect() - Fatal error: Uncaught Error: Call to undefined function mysql_connect() Docker + Wordpress - 收到此错误“致命错误:未捕获错误:调用未定义函数 mysql_connect()” - Docker + Wordpress - get this error "Fatal error: Uncaught Error: Call to undefined function mysql_connect()" 致命错误:未捕获错误:调用 C:\\xampp\\htdocs\\ 中未定义的函数 mysql_connect() - Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\xampp\htdocs\ 使用PHP创建登录页面:致命错误:未捕获错误:调用未定义函数mysql_connect() - Create login page using PHP: Fatal error: Uncaught Error: Call to undefined function mysql_connect() 致命错误:调用未定义函数mysql_connect()错误 - Fatal error: Call to undefined function mysql_connect() error 致命错误:调用未定义函数mysql_connect() - Fatal error: Call to undefined function mysql_connect() in PHP致命错误:调用未定义函数mysql_connect() - PHP Fatal error : Call to undefined function mysql_connect() 致命错误:调用未定义函数mysql_connect() - Fatal error: Call to undefined function mysql_connect() 致命错误:调用未定义函数mysql_connect()无法解决 - Fatal error: Call to undefined function mysql_connect() cannot solve 致命错误:调用未定义的函数mysql_connect() - Fatal error: Call to undefined function mysql_connect()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM