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”.

    <?php

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
                        ,"username"=>$user->name
                        ,"email"=>$user->email
                        ,"pass"=>$user->pass
                        ,"birthday"=>$user->birthday
                        ,"gender"=>$user->gender
                        ,"address"=>$user->address
                        ,"city"=>$user->city
                        ,"postalCode"=>$user->postalCode
                        ,"phoneNumber"=>$user->phoneNumber
                        ,"bloodType"=>$user->bloodType
                        ,"diabetesType"=>$user->diabetesType
                        ,"weight"=>$user->weight
                        ,"height"=>$user->height);
}

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

EDIT:
my conexao.php is like this:

    <?php
   $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

Does

<?php
$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
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_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
ty