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

$dbhost_name = "";
$database = "";
$username = "";
$password = "";

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

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(';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?


1 Like

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