Replacement for deprecated function mysql_connect [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:20:02

问题:

This question already has an answer here:

So I have this Amazon Web Service database all set up

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.

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.

回答1:

The documentation for PHP says

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension 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); } 


回答2:

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

As several others have mentioned, the supported alternatives to the mysql extension are mysqli and PDO MySQL.



回答3:

MysqlI (mysql improved) is the successor for mysql.

$Link = new mysqli($Hostname, $Username, $Password, $Database); $Result = $Link->query('SELECT ...'); $Rows = $Result->fetch_array(MYSQLI_NUM); 


回答4:

The manual suggests mysqli as an alternative http://php.net/manual/en/book.mysqli.php



回答5:

One easy alternative to the MySQL extension is MySQLi. Instead of

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/



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!