I am trying to connect to a remote mysql server using PDO php script. Script is working fine when run from the terminal.I am able to connect to the remote mysql database.
function db_connect($host, $username, $password, $db_name){
global $conn;
try{
$conn = new PDO("mysql:host=$host;dbname=$db_name", $username, $password);
$conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "success";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
}
But when I run this php script from browser, I got following error-
ERROR:SQLSTATE[HY000] [2003] Can't connect to MySQL server
First I though it is because I am running the script from root user. But I tested it with a test user and it still runs from terminal but not from the browser. I am also able to connect to the database from mysql workbench. I don't know the cause. Please suggest some solution.