1

I'm creating a web application in PHP and I need to connect to a database and retrieve information from it. The database in question is being hosted on phpMyAdmin. I'm currently using the following PHP code to connect to it.

//Attempt to connect to the database
$conn = mysql_connect('localhost', 'my_username', 'my_password') or die (mysql_error());

//Tell the user if they were successful
echo "Connection successful!";

//Close the connection
mysql_close();

When I run the website, it produces the following SQL error:

"No connection could be made because the target machine actively refused it."

I'm sure that my username and password are spelled correctly and I believe that 'localhost' is the server name that I need to use. Is there a different mysql_connect command that I need to use for phpMyAdmin? If not, how can I solve this problem?

Edit:

Upon publishing the website to Microsoft Azure (where I need to host it), I've found that it produces a different error:

"An attempt was made to access a socket in a way forbidden by its access permissions."

How will I be able to fix this error? Or will fixing the original error also solve this one?

Ben
  • 1,299
  • 3
  • 17
  • 37

2 Answers2

2

Try use IP instead of localhost.

User has permissions? Check them

Zhenya
  • 271
  • 1
  • 4
  • 15
2
  1. Do not use mysql_* functions. They are deprecated. For more information, see this question: Why shouldn't I use mysql_* functions in PHP?

  2. phpMyAdmin hosts nothing; it is simply a PHP application that connects to your database just like your own app is attempting to do.

  3. Take a look at the phpMyAdmin config file and ensure you are using the same host. Also try the same username/password. If this succeeds, it's advisable to set up a dedicated username/password for your application. This can be done within phpMyAdmin on the privileges page.

Community
  • 1
  • 1
rjdown
  • 9,162
  • 3
  • 32
  • 45