php 替换已弃用的函数 mysql_connect
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16531252/
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
Replacement for deprecated function mysql_connect
提问by Juan Mendes
So I have this Amazon Web Service database all set up
所以我已经设置了这个 Amazon Web Service 数据库
I'm working off an old tutorial for an application that I'm planning on using it with.
我正在为我计划使用它的应用程序编写一个旧教程。
I noticed that mysql_connect was deprecate when I looked it up.
我注意到 mysql_connect 在我查找时已被弃用。
What can I use as an alternative? How can I connect to my amazon database?
我可以用什么作为替代?如何连接到我的亚马逊数据库?
<?
mysql_connect("localhost", "username", "password") or die ("Can't connect to database server");
mysql_select_db("videoRecorderDemo") or die ("Can't select database");
mysql_query("SET NAMES 'utf8'");
?>
I get this error:
我收到此错误:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'username'@'192.168.1.1' (using password: YES) in /www/zxq.net/r/e/d/red5vptest/htdocs/utility/db.php on line 2
Can't connect to database server
No matter what credentials I use. It also doesn't help that Amazon's code samples show you connecting in an entirely different way.
无论我使用什么凭据。亚马逊的代码示例显示您以完全不同的方式进行连接也无济于事。
回答by Juan Mendes
The documentation for PHP says
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLior PDO_MySQLextension should be used.
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
/*
* This is the "official" OO way to do it,
* BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
*/
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
回答by duskwuff -inactive-
The error message you are getting is unrelated to the deprecation of mysql_connect
. The MySQL username and password you're using ("username" and "password" in your code) are wrong — if you don't know the correct values, check the documentation for the version of MySQL you installed. (You may need to create a user and/or database.)
您收到的错误消息与mysql_connect
. 您使用的 MySQL 用户名和密码(代码中的“用户名”和“密码”)是错误的——如果您不知道正确的值,请查看您安装的 MySQL 版本的文档。(您可能需要创建用户和/或数据库。)
As several others have mentioned, the supported alternatives to the mysql extension are mysqliand PDO MySQL.
回答by peku33
MysqlI (mysql improved) is the successor for mysql.
MysqlI(mysql改进版)是mysql的继承者。
$Link = new mysqli($Hostname, $Username, $Password, $Database);
$Result = $Link->query('SELECT ...');
$Rows = $Result->fetch_array(MYSQLI_NUM);
回答by nickisme
One easy alternative to the MySQL extension is MySQLi. Instead of
MySQL 扩展的一种简单替代方案是 MySQLi。代替
mysql_connect("host", "username", "password")
... you could connect using e.g.
...您可以使用例如连接
$db = mysqli_connect("host", "username", "password");
It's probably worth reading this helpful tutorial: http://phpmaster.com/avoid-the-original-mysql-extension-1/
可能值得阅读这个有用的教程:http: //phpmaster.com/avoid-the-original-mysql-extension-1/
回答by nullpointerexception
The manual suggests mysqli as an alternative http://php.net/manual/en/book.mysqli.php
手册建议 mysqli 作为替代 http://php.net/manual/en/book.mysqli.php