1

I was trying to connect with my database...

So i have added all the corresponding code, when run index.php.

It shows,

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

index.php :

  <?php

   $user = 'root';
   $pass ='';

try {
    $dbh = new PDO('mysql:host=localhost;dbname=marudhar_db', $user, $pass);
    echo "go go go...!!!!";
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

?>
Mann Meena
  • 13
  • 1
  • 7
  • Possible duplicate of [php: SQLSTATE\[HY000\] \[2002\] No connection could be made because the target machine actively refused it](http://stackoverflow.com/questions/25609491/php-sqlstatehy000-2002-no-connection-could-be-made-because-the-target-machi) – ventiseis Mar 13 '16 at 19:09
  • 1
    Not a duplicate; http://stackoverflow.com/questions/25609491/php-sqlstatehy000-2002-no-connection-could-be-made-because-the-target-machi mentions an explicit client-side specification of a nonstandard port. – O. Jones Mar 13 '16 at 20:57

2 Answers2

1

The "target machine actively refused it" message means you attempted a TCP Connect operation to a port on a machine where no server software was listening.

In the case of a MySQL server on localhost it could mean a few things

  1. The MySQL server isn't running at all
  2. The MySQL server is configured to use some nonstandard port number for connections. 3306 is the standard one.
  3. The MySQL server is not listening on the localhost interface (127.0. 0.1) but only on the network interface.

The last two are unlikely unless you've been experimenting with the my.cnf my file.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • one thing more... Whenever you open connections in the server-side language, do not forget to close the Connection after work-completion.This is also one of the most common issue when new comers come in – Asif Mehmood Mar 13 '16 at 19:41
0

I tried the same code. I created database with name 'marudhar_db' and run the code. It works perfectly and prints go go go. Check your wamp or xamp connection is proper or not. either restart your wamp /xamp once.

Passionate Coder
  • 7,154
  • 2
  • 19
  • 44