I need to connect to a remote MySQL server with PHP but I get the following error:
Warning: mysqli::mysqli(): (HY000/2003): Can't connect to MySQL server on ... (13) in /var/www/html/index.php on line 16
Warning: mysqli::query(): Couldn't fetch mysqli in /var/www/html/index.php on line 17
Fatal error: Call to a member function fetch_assoc() on a non-object in /var/www/html/index.php on line 18
The remote server firewal is open, user has permission to connect from any Host, and I'm able to connect from the local server with mysql command line, but not able to connect with PHP.
What may be the problem here?
EDIT: Already tried with different connection code, but always same error.
This is the latest one:
$mysqli = new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$result = $mysqli->query("SELECT 'Hello, dear MySQL user!' AS msg");
$row = $result->fetch_assoc();
echo $row['msg'];
Found this to be an issue with SELinux that is not allowing httpd network connections.
Executing:
setsebool -P httpd_can_network_connect=1
Solved the problem.
Make sure you inserted information In the connection info. Replace DB_DATABASE and other keywords with your database info in single quotes.
$mysqli = new mysqli('localhost', 'MyUsername', ''mypassword', 'database1');