How to connect to a mysql database from another network via PHP?

-1

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     

asked by anonymous 26.11.2018 / 11:03

1 answer

0

Try this:

 <?php

    $host="hostname";
    $username="user";
    $password="pass";
    $dbname="banco";
    $port="3306";
    /

    try {
        $dbh = new PDO("mysql:host=$host;port=$port;dbname=$dbname;charset=utf8", $username, $password);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "I am connected.<br/>";




    } catch (PDOException $e) {
        echo 'PDO Exception: ' . $e->getMessage();
        exit();
    }
?>

You need to make sure your bank is in the url that is passing and that the port is correct.

    
26.11.2018 / 11:53