I would like to make a PHP connection to the MYSQL database that is on another computer from another network. I have the external IP of this machine that hosts the database and I made the following PHP connection:
<?php
$servidor = 'servidor_externo:porta';
$banco = 'bancp';
$usuario = 'usuario';
$senha = 'senha';
$link = mysqli_connect($servidor, $usuario, $senha);
$db = mysqli_select_db($banco,$link);
if(!$link)
{
echo "erro ao conectar ao banco de dados!";
exit();
}
?>
However, it does not work ... I tried another way:
<?php
// Informações para conexão
$host = 'ipexterno';
$usuario = 'usuario';
$senha = 'senha';
$banco = 'banco';
$dsn = "mysql:host={$host};port=3004;dbname={$banco};charset=utf8";
try
{
// Conectando
$pdo = new PDO($dsn, $usuario, $senha);
}
catch (PDOException $e)
{
// Se ocorrer algum erro na conexão
die($e->getMessage());
}
With this code you get the following error message SQLSTATE [HY000] [2006] MySQL server has gone away