PHP Connection failed SQLSTATE HY000 2002 Connection refused

0 votes

I am trying to connect MySQL Database which is on PHPMyAdmin.using PHP. I am using MAMP to host the database, the connection I am trying to use is this:

<?php
$servername = "127.0.0.1";
$username = "root";
$password = "root";

try {
    $conn = new PDO("mysql:host=$servername;dbname=AppDatabase", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
}
catch(PDOException $e)
{
    echo "Connection failed: " . $e->getMessage();
}
?>

I keep receiving this error when I try to test if the connection is working or not using postman:

Connection failed: SQLSTATE[HY000] [2002] Connection refused

How can I make this work?

Jun 24, 2022 in PHP by narikkadan
• 63,680 points
3,367 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In PHP

0 votes
1 answer

Error:PDOException SQLSTATE[HY000] [2020] No such file or directory in php?

Hello, I checked php -i | grep pdo and noticed ...READ MORE

answered Apr 2, 2020 in PHP by Niroj
• 82,880 points
1,387 views
0 votes
1 answer

Connection with MySQL server using PHP. How can we do that?

Hey @kartik, You have to provide MySQL hostname, ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,880 points
1,268 views
0 votes
1 answer

Error:“Connection for controluser as defined in your configuration failed” with phpMyAdmin in XAMPP

Hii, I edited /etc/phpmyadmin/config.inc.php and changed the following ...READ MORE

answered Apr 20, 2020 in PHP by Niroj
• 82,880 points
15,229 views
0 votes
0 answers

PHP - Failed to open stream : No such file or directory

In PHP scripts, when I call include(), require(), fopen(), or ...READ MORE

May 16, 2022 in PHP by Kichu
• 19,050 points
7,078 views
0 votes
0 answers

PDOException SQLSTATE[HY000] [2002] No such file or directory

My (very basic) site appears to have ...READ MORE

Sep 3, 2022 in Database by Kithuzzz
• 38,020 points
2,033 views
0 votes
1 answer
0 votes
2 answers

Define a SQL query? What is the difference between SELECT and UPDATE Query? How do you use SQL in SAS?

HI.. SQL is Structured Query Language, which is ...READ MORE

answered Aug 8, 2020 in PHP by anonymous
10,470 views
0 votes
1 answer

Why is not preferable to use mysql_* functions in PHP?

The reasons are as follows: The MySQL extension: Does ...READ MORE

answered Sep 7, 2018 in Database by DataKing99
• 8,250 points
1,238 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP