Undefined variable: PDO

Hey, i uploaded a php script to query my database. It worked while it was local (XAMPP) but in 000webhost i get the error “Undefined variable: PDO”.


include "conexao.php";

$sql_read = "SELECT * FROM users";

$dados = $PDO->query($sql_read);

$resultado = array();

while ($user = $dados->fetch(PDO::FETCH_OBJ))    {

    $resultado[] = array("userID"=>$user->userID

echo json_encode($resultado);


Are you sure you uploaded conexao.php?

I did, the login works fine because i dont use PDO but all the other scripts that use PDO dont work

my conexao.php is like this:

   $dns = "mysql:localhost;dbname=id9916456_appprojeto;charset=utf8";
    $utilizador = "id9916456_lemon";
    $senha = "XXXXXXX";
    try {
        $PDO = new PDO($dns, $utilizador, $senha);
    } catch (PDOException $erro) {


Maybe its the $dsn. In the camp mysql i put localhost like it says on the website or the link to the website?

It’s probably failing to connect, add some error logger to the catch block


$servername = "localhost";
$username = "username";
$password = "password";
$database = "database";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
    // set the PDO error mode to exception
    echo "Connected successfully"; 
    } catch(PDOException $e) {    
    echo "Connection failed: " . $e->getMessage();

Work for you?

1 Like

found the problem, i was missing “:host” after mysql