I have the following PHP code to connect to my db:
Maybe your MySQL is not using TCP for localhost. Please try
$host='/tmp/mysql.sock';
or whatever socket it might be using.