<?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 don't understand why this error comes out, since the PDO parameters are correct (I'm not sure about the port, but I've already tried several and it still doesn't work).
The response you receive indicates that it receives absolutely no response, you can rule out authentication problems. Make sure you are connecting to the correct host and port. If so, check the firewall of the host and/or machine you want to connect from. What Alfonso told you is also very important, check that the mysql configuration of the host allows external connections.
A good way to help you diagnose the problem is to try to connect manually with some software mysql client, for example Heidisql
http://www.heidisql.com/
Download it and try to connect to the same database, if you can't it effectively means that host address, port or mysql configuration needs to be checked.
All the best
From the URL format you're connecting to, the mysql database appears to be hosted on a different server than where the PHP files are:
You should find out from the hosting provider if the IP from where you are trying to connect is enabled to make remote MySQL connections to the server where the database is hosted.
If you are working from localhost, it is very likely that you will not be able to connect either.
If the server where the database is is the same where the code is, it is recommended to use "localhost"... this error disappeared with me because the hosting service never notified when there was an IP change; on the other hand, it would be nice if you could get the IP of the URL you indicate as host