-2

I was printing the mysql_get_server_info() using like this:

<?php 
   echo 'MySQL Informtion';
   echo mysql_get_server_info();
?>

but got

PHP warning mysql_get_server_info(): Access denied for user 'www-data'@'localhost (using password: NO)

in response. I am using PHP5.6. what causing this and how do i fix it.

NikiC
  • 100,734
  • 37
  • 191
  • 225
Kuldeep Dangi
  • 4,126
  • 5
  • 33
  • 56
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Dec 29 '14 at 10:31
  • Its an old project I am looking at and for the time being I cant replace everywhere with mysqli – Kuldeep Dangi Dec 29 '14 at 10:35
  • If you create a minimal example, from your code in question, you'll probably discover the issue. As others have illustrated, your posted code does reveal the problem. – Sonny Dec 30 '14 at 18:41

2 Answers2

0

From the documentation:

link_identifier: The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.

To fix it, you need to run mysql_connect (with the correct username, password, etc.) before you call mysql_get_server_info.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

The problem is that you are not allowed to interact with the database server using the applied default settings.

From the documentation of mysql_get_server_info([$link_identifier]):

link_identifier: The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.

As you did not specify any connection and likely have not opened one before (at least your code does not specify it), a connection is automatically created using mysql_connect with default parameters. If SQL safe mode is active, the user name is the owner of the server process (which you cannot override), else, the user name is specified in mysql_default_user, which you can override by specifying a user.

To fix the issue, either explicitly open a connection to the server using mysql_connect or modify the database configuration so that you can use the implicitly created connection.

BTW: The mysql API of PHP is deprecated in favor of mysqli or PDO.

Abrixas2
  • 3,207
  • 1
  • 20
  • 22
  • Then the authentication used in that connection does not have sufficient privileges. Thus, you either need to open another connection with an authentication that has enough privileges or you need to modify the database configuration. – Abrixas2 Dec 29 '14 at 10:45