简体   繁体   English

弃用:mysql_connect()

[英]Deprecated: mysql_connect()

I am getting this warning, but the program still runs correctly.我收到此警告,但程序仍能正常运行。

The MySQL code is showing me a message in PHP: MySQL 代码在 PHP 中向我显示了一条消息:

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\\xampp\\htdocs\\task\\media\\new\\connect.inc.php on line 2已弃用:mysql_connect():mysql 扩展已弃用,将来会删除:在 C:\\xampp\\htdocs\\task\\media\\new\\connect.inc.php 第 2 行中使用 mysqli 或 PDO 代替

My connect.inc.php page is我的connect.inc.php页面是

<?php
  $connect = mysql_connect('localhost','root','');
  mysql_select_db('dbname');
?>

What does this mean and how can I eliminate the message?这是什么意思,我该如何消除该消息?

There are a few solutions to your problem.您的问题有几种解决方案。

The way with MySQLi would be like this: MySQLi 的方式是这样的:

<?php
$connection = mysqli_connect('localhost', 'username', 'password', 'database');

To run database queries is also simple and nearly identical with the old way:运行数据库查询也很简单,几乎与旧方式相同:

<?php
// Old way
mysql_query('CREATE TEMPORARY TABLE `table`', $connection);
// New way
mysqli_query($connection, 'CREATE TEMPORARY TABLE `table`');

Turn off all deprecated warnings including them from mysql_*:关闭所有已弃用的警告,包括来自 mysql_* 的警告:

<?php
error_reporting(E_ALL ^ E_DEPRECATED);

The Exact file and line location which needs to be replaced is "/System/Startup.php > line: 2 " error_reporting(E_All);需要替换的确切文件和行位置是“/System/Startup.php > line: 2” error_reporting(E_All); replace with error_reporting(E_ALL ^ E_DEPRECATED);替换为 error_reporting(E_ALL ^ E_DEPRECATED);

You can remove the warning by adding a '@' before the mysql_connect.您可以通过在 mysql_connect 之前添加“@”来删除警告。

@mysql_connect('localhost','root','');

but as the warning is telling you, use mysqli or PDO since the mysql extension will be removed in the future.但是正如警告告诉您的那样,请使用 mysqli 或 PDO,因为将来会删除 mysql 扩展。

To suppress the deprecation message for this alone (and stay informed of other deprecations in your code) you can prefix the connect with @:要单独取消弃用消息(并随时了解代码中的其他弃用信息),您可以在连接前加上@:

<?php
$connect = @mysql_connect('localhost','root','');
mysql_select_db('dbname');
?> 

Deprecated features in PHP 5.5.x PHP 5.5.x 中已弃用的功能

The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database.原始 MySQL 扩展现已弃用,并且在连接到数据库时会生成E_DEPRECATED错误。 Instead, use the ** MYSQLi or PDO_MySQL extensions.**相反,使用 ** MYSQLiPDO_MySQL扩展。**

Syntax:句法:

<?php
  $connect = mysqli_connect('localhost', 'user', 'password', 'dbname');

Also, replace all mysql_* functions into mysqli_* functions另外,将所有mysql_*函数替换为mysqli_*函数

instead of代替

<?php
 $connect = mysql_connect('localhost','root','');
  mysql_select_db('dbname');
?> 

This warning is displayed because a new extension has appeared.显示此警告是因为出现了新扩展。 It suppouse that you still can use the old one but in some cases it´s impossible.假设您仍然可以使用旧的,但在某些情况下这是不可能的。

I show you how I do the connection with database.我向您展示我如何与数据库建立连接。 You need just change the values of the variables.您只需要更改变量的值。

My connection file: connection.php我的连接文件: connection.php

<?php    
 $host='IP or Server Name (usually "localhost") ';
 $user='Database user';
 $password='Database password';
 $db='Database name';

 //PHP 5.4 o earlier (DEPRECATED)
 $con = mysql_connect($host,$user,$password) or exit("Connection Error");
 $connection = mysql_select_db($db, $con);

 //PHP 5.5 (New method)
 $connection =  mysqli_connect($host,$user,$password,$db);
?>

The extension changes too when performing a query.执行查询时,扩展名也会发生变化。

Query File: "example.php"查询文件: “example.php”

<?php
 //First I call for the connection
 require("connection.php");

 // ... Here code if you need do something ...

 $query = "Here the query you are going to perform";

 //QUERY PHP 5.4 o earlier (DEPRECATED)
 $result = mysql_query ($query) or exit("The query could not be performed");

 //QUERY PHP 5.5 (NEW EXTENSION)
 $result = mysqli_query ($query) or exit("The query could not be performed");    
?>

This way is using MySQL Improved Extension , but you can use PDO (PHP Data Objects) .这种方式是使用MySQL 改进的扩展,但您可以使用PDO (PHP 数据对象)

First method can be used only with MySQL databases, but PDO can manage different types of databases.第一种方法只能用于 MySQL 数据库,但 PDO 可以管理不同类型的数据库。

I'm going to put an example but it´s necessary to say that I only use the first one, so please correct me if there is any error.我将举一个例子,但有必要说我只使用第一个,所以如果有任何错误,请纠正我。

My PDO connection file: "PDOconnection.php"我的 PDO 连接文件: “PDOconnection.php”

<?php
 $hostDb='mysql:host= "Here IP or Server Name";dbname="Database name" ';
 $user='Database user';
 $password='Database password';

 $connection = new PDO($hostDb, $user, $password);
?>

Query File (PDO): "example.php"查询文件 (PDO): “example.php”

<?php
 $query = "Here the query you are going to perform";
 $result=$connection->$query;
?>

To finish just say that of course you can hide the warning but it´s not a good idea because can help you in future save time if an error happens (all of us knows the theory but if you work a lot of hours sometimes... brain is not there ^^ ).最后只是说当然你可以隐藏警告,但这不是一个好主意,因为如果发生错误,它可以帮助你在未来节省时间(我们都知道这个理论,但如果你有时工作很多小时...... . 脑子不在那里^^)。

That is because you are using PHP 5.5 or your webserver would have been upgraded to 5.5.0.那是因为您使用的是 PHP 5.5,否则您的网络服务器会升级到 5.5.0。

The mysql_* functions has been deprecated as of 5.5.0 mysql_*函数已从 5.5.0 开始弃用

在此处输入图片说明

Source

mysql_*, is officially deprecated as of PHP v5.5.0 and will be removed in the future. mysql_*,自 PHP v5.5.0 起正式弃用,将来会被删除。

Use mysqli_* function or pdo使用mysqli_*函数或pdo

Read Oracle Converting to MySQLi阅读 Oracle转换为 MySQLi

Its just a warning that is telling you to start using newer methods of connecting to your db such as pdo objects它只是一个警告,告诉您开始使用连接到数据库的新方法,例如 pdo 对象

http://code.tutsplus.com/tutorials/php-database-access-are-you-doing-it-correctly--net-25338 http://code.tutsplus.com/tutorials/php-database-access-are-you-doing-it-correctly--net-25338

The manual is here手册在这里

http://www.php.net/manual/en/book.pdo.php http://www.php.net/manual/en/book.pdo.php

Warning "deprecated" in general means that you are trying to use function that is outdated.警告“已弃用”通常意味着您正在尝试使用过时的函数。 It doeasnt mean thaqt your code wont work, but you should consider refactoring.这并不意味着您的代码将无法工作,但您应该考虑重构。

In your case functons mysql_ are deprecated.在您的情况下,函数 mysql_ 已弃用。 If you want to know more about that here is good explanation already : Why shouldn't I use mysql_* functions in PHP?如果您想了解更多信息,这里已经有很好的解释: 为什么我不应该在 PHP 中使用 mysql_* 函数?

<?php 
$link = mysqli_connect('localhost','root',''); 
if (!$link) { 
    die('Could not connect to MySQL: ' . mysqli_error()); 
} 
echo 'Connection OK'; mysqli_close($link); 
?>

This will solve your problem.这将解决您的问题。

Well, i just faced such message today when i moved to new hosting!好吧,我今天刚搬到新主机时遇到了这样的消息! anyway i have tried to change the "mySQL" to "mySQLi" but not working, so i have done this:无论如何,我已尝试将“mySQL”更改为“mySQLi”但不起作用,所以我这样做了:

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
# Turn off all error reporting
error_reporting(0);
$connect_myconn = "Database Connection";
$hostname_myconn = "localhost";
$database_myconn = "db name";
$username_myconn = "user name";
$password_myconn = "pass";
$myconn = mysql_connect($hostname_myconn, $username_myconn, $password_myconn) or die("<h1 style=margin:0;>A MySQL error has occurred.</h1><p><b>Your Query:</b> " . $connect_myconn . "<br /> <b>Error Number:</b> (" . mysql_errno() . ")</p>" . mysql_error());
mysql_select_db($database_myconn, $myconn);
?>

The trick is to set error reporting off :)诀窍是关闭错误报告:)

# Turn off all error reporting
error_reporting(0);

For PHP 7+ you can use this code instead:对于 PHP 7+,您可以使用以下代码:

ini_set('display_errors', 0);
ini_set('log_errors', 1);

Thanks谢谢

PDO class replaces these methods. PDO 类取代了这些方法。 Example for Mysql or MariaDB : Mysql 或 MariaDB 的示例:

$BDD_SQL = new PDO('mysql:host='.BDD_SQL_SERVER.';dbname='.BDD_SQL_BASE.';charset=utf8', 
        BDD_SQL_LOGIN, BDD_SQL_PWD, 
        array(
          PDO::ATTR_EMULATE_PREPARES => false,
          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //launch exception if error
          PDO::ATTR_DEFAULT_FETCH_MODE=> PDO::FETCH_ASSOC
                ));

Source : PDO Class来源: PDO 类

If you have done your coding then如果你已经完成了编码,那么

ini_set("error_reporting", E_ALL & ~E_DEPRECATED); 

is good option but if you are in beginning then definitely you should use mysqli.是不错的选择,但如果您刚开始,那么您肯定应该使用 mysqli。

把它放在你的php页面中。

ini_set("error_reporting", E_ALL & ~E_DEPRECATED); 

Adding a @ works for me!添加@对我有用!

I tested with error_reporting(E_ALL ^ E_DEPRECATED);我测试了error_reporting(E_ALL ^ E_DEPRECATED);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM