10

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.

  • There's a couple of options for you - either of the options mentioned in [this question](http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons). Yes, if AWS docs use `mysql` library examples, well they shouldn't! `:)`. – halfer May 13 '13 at 21:18

5 Answers5

12

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);
}
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
5

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.

2

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);
peku33
  • 3,628
  • 3
  • 26
  • 44
1

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/

nickisme
  • 83
  • 1
  • 8
0

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