Error in web service: Error: SQLSTATE [HY000] [2002] Connection refused

1
<?php

$usu = $_REQUEST['usu'];
$pas = $_REQUEST['pas'];

try{
    // establecemos la conexion con PDO
    $conexion = new PDO("mysql:host=pagina.000webhostapp.com;port=3306;dbname=id554244_basededatos", "id453244_usuario", "contraseña");

    $conexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // establecemos el juego de caracteres utf8
    $conexion->exec("SET CHARACTER SET utf8");

    // consulta para obtener el alumno con dicho usuario y contraseña
    $consulta_sql = "SELECT * FROM alumnos WHERE codAlu=? AND pasUsu=?";

    // preparamos la consulta
    $resultado = $conexion->prepare($consulta_sql);

    // ejecutamos la consulta con los aparametros del array, entrecomillado porque son varchar
    $resultado->execute(array("$usu", "$pas"));// guardamos en resultado la tabla virtual que devele la consulta

    $datos = array();

    foreach($resultado as $row){
        $datos[] = $row;
    }

    echo json_encode($datos);

}catch(Exception $e){
    die("Error: " . $e->GetMessage());
}finally{// esto se ejecuta haya o no error
    $conexion = null;
}

?>

I do not understand why this error comes out, since the parameters of the PDO are correct (I am not sure about the port, but I have already tried several and it still does not work).

    
asked by Cristian Alvarez Hernandez 17.10.2016 в 07:31
source

2 answers

1

Because of the URL format to which you are connecting, the mysql database seems to be hosted on a different server than where the PHP files are:

host=pagina.000webhostapp.com;

You should check with the hosting provider, if the IP from where you are trying to connect is enabled to make connections from MySQL remote to the server where the database is hosted.

If you are working from localhost, it is very likely that you can not connect either.

    
answered by 17.10.2016 / 15:31
source
1

The response you receive indicates that you receive absolutely no response, you can rule out authentication problems. Make sure you are connecting to the appropriate host and port. If so, check the firewall of the host and / or the machine from which you want to connect. Very important also what Alfonso told you, check that the mysql configuration of the host allows external connections.

A good way to help diagnose the problem is to try to connect by hand with a mysql software client, for example Heidisql

link

Download it and try to connect to the same database, if you do not get it it effectively means that the host address, port or mysql configuration needs to be reviewed.

Greetings

    
answered by 19.10.2016 в 09:31