php Deprecated: mysql_connect(): mysql 扩展被弃用,将来会被移除:使用 mysqli 或 PDO
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26703171/
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
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO
提问by user2308205
I am new to PHP and im trying to connect to my database whith the help of some you tube videos but i get this error
我是 PHP 新手,我试图在一些管视频的帮助下连接到我的数据库,但我收到此错误
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\db.php on line 7
已弃用:mysql_connect():mysql 扩展已弃用,将来会被删除:在第 7 行的 C:\wamp\www\db.php 中使用 mysqli 或 PDO 代替
please help ..i am confused !!!
请帮助..我很困惑!
回答by Funk Forty Niner
The problem stems from Wampserver's demo SQL files which include mysql_*
based functions code.
问题源于Wampserver的演示 SQL 文件,其中包括mysql_*
基于函数的代码。
Sidenote:They really should make a note of that or update their demo files to include test files containing mysqli_
and/or PDO code to leave out the confusion, since the version of PHP that comes with it is 5.5.12, which would only make sense.
旁注:他们真的应该记下这一点或更新他们的演示文件以包含包含mysqli_
和/或 PDO 代码的测试文件以避免混淆,因为它附带的 PHP 版本是 5.5.12,这才有意义.
I myself have recently installed Wamp in one my machines a few weeks ago and was faced with the very same issue, yet I quickly remedied the situation by simply changing all instances of mysql_
to mysqli_
and setting the DB connection variable as the first parameter.
几周前,我自己最近在我的一台机器上安装了 Wamp,并面临着同样的问题,但我通过简单地更改mysql_
to 的所有实例mysqli_
并将数据库连接变量设置为第一个参数来迅速解决了这种情况。
For example and taken from http://php.net/manual/en/function.mysqli-connect.php
例如,取自http://php.net/manual/en/function.mysqli-connect.php
$result = mysqli_query($link, $query); // $link being the connection variable
This is what their demo SQL code looks like:
这是他们的演示 SQL 代码的样子:
<?php
$link = mysql_connect('hostname','dbuser','dbpassword');
if (!$link) {
die('Could not connect to MySQL: ' . mysql_error());
}
echo 'Connection OK'; mysql_close($link);
?>
Change it to the following as an example and changing the proper code for your own DB:
将其更改为以下示例并为您自己的数据库更改正确的代码:
<?php
$link = mysqli_connect('hostname','dbuser','dbpassword','db_name');
if (!$link) {
die('Could not connect to MySQL: ' . mysqli_error($link));
}
echo 'Connection OK'; mysqli_close($link);
?>
For more information on mysqli_
and PDO, visit the following pages:
有关mysqli_
PDO 的更多信息,请访问以下页面:
Additional links:
附加链接:
which are much better and safer to use when getting into database work.
在进入数据库工作时使用它们更好更安全。
回答by Adnan ul-haq
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\gazal156\system\database\mysql.php on line 6
已弃用:mysql_connect():mysql 扩展已弃用,将来会被删除:在 C:\wamp\www\gazal156\system\database\mysql.php 中第 6 行使用 mysqli 或 PDO 代替
- I m use opencart 1.5.6 but run open cart site then show error.
- Error in the simple solution.
- 我使用 opencart 1.5.6 但运行打开购物车站点然后显示错误。
- 简单解决方案中的错误。
../system/database/mysql.php
../system/database/mysql.php
Top addline after
顶部添加行
error_reporting(E_ALL ^ E_DEPRECATED);
错误报告(E_ALL ^ E_DEPRECATED);
and save
并保存
回答by Ahmed Magdy
PHP 5 and later can work with a MySQL database using:
PHP 5 及更高版本可以使用以下方法处理 MySQL 数据库:
MySQLi extension (the "i" stands for improved)
MySQLi 扩展(“i”代表改进)
PDO (PHP Data Objects)
PDO(PHP 数据对象)
Earlier versions of PHP used the MySQL extension. However, this extension was
早期版本的 PHP 使用 MySQL 扩展。然而,这个扩展是
deprecated in 2012.
2012 年弃用。
Should I Use MySQLi or PDO?
我应该使用 MySQLi 还是 PDO?
If you need a short answer, it would be "Whatever you like".
如果你需要一个简短的答案,那就是“随便你”。
Both MySQLi and PDO have their advantages:
MySQLi 和 PDO 都有其优点:
PDO will work on 12 different database systems, where as MySQLi will only work with MySQL databases.
PDO 将适用于 12 种不同的数据库系统,而 MySQLi 仅适用于 MySQL 数据库。
So, if you have to switch your project to use another database, PDO makes the process easy. You only have to change the connection string and a few queries. With MySQLi, you will need to rewrite the entire code - queries included. Both are object-oriented, but MySQLi also offers a procedural API. Both support Prepared Statements. Prepared Statements protect from SQL injection, and are very important for web application security.
因此,如果您必须将项目切换为使用另一个数据库,PDO 使该过程变得简单。您只需要更改连接字符串和一些查询。使用 MySQLi,您将需要重写整个代码 - 包括查询。两者都是面向对象的,但 MySQLi 还提供了一个过程 API。两者都支持准备好的报表。Prepared Statements 可以防止 SQL 注入,并且对于 Web 应用程序安全非常重要。
Example (MySQLi Object-Oriented)
示例(MySQLi 面向对象)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
source : http://www.w3schools.com/php/php_mysql_connect.aspfor more information
来源:http: //www.w3schools.com/php/php_mysql_connect.asp了解更多信息