Error!: SQLSTATE[HY000] [2002]

I get the error below whilst I try and set up a AJAX scripted tool on my website, where a user will start to type into a search area and the ajax looks into the database to pull out the results, but I get this error.

Error!: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it. 

Here is my code where I think the error is being created

<?Php
$dbhost_name = "";
$database = "";
$username = "";
$password = "";

try {
$dbo = new PDO('mysql:host=localhost;dbname='.$database, $username, $password);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>

Did you clear these out to post (not a bad choice, tbh). But if you didn’t clear it out, that’s the problem. They need to hold the connection information to your database.

Yes I did clear them out to post sorry.

That’s fine. Just making sure. Just be sure the info in there is correct. 90% of the time it’s a typo of some kind.

Yep just checked them again and the details are def correct

I got it, silly me…

I left localhost in instead of below -

$dbo = new PDO('mysql:host=localhost;dbname='.$database, $username, $password);

$dbo = new PDO('mysql:host=mysql101.ecristal.co.uk;dbname='.$database, $username, $password);
1 Like

Shouldn’t this

$dbo = new PDO('mysql:host=localhost;dbname='.$database, $username, $password);

be this ?

$dbo = new PDO('mysql:host=' . $dbhost_name . ';dbname='.$database, $username, $password);
1 Like

Yep spot on, just doing it now

… thread be in the php forum?

:crying_cat_face:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.