Why the error in PDO Uncaught Error: Call to undefined method PDO :: fetch ()?

1

I do not know why this error occurred.

  PDA :: fetch () in /opt/lampp/htdocs/myschool.com.br/login.php:24 Stack trace: # 0 {main} thrown in / opt /lampp/htdocs/myschool.com.br/login.php on line 24

PHP

use dao\Connection;

session_start();
//Incluir a conexão com banco de dados
include 'application/dao/Connection.php';
date_default_timezone_set("Etc/GMT+3");

if(!empty($_POST)){
    $res='';
    $email = $_POST['email'];
    $senha = $_POST['password'];

    $sql = "SELECT idlog, logname 
            FROM login 
            WHERE logemail='$email' AND logpassword=md5('$senha')
                AND logBlocked = 'no' AND logstatus = 1;";
    $res = Connection::getInstance();
    $res->prepare($sql);
    $res->exec($sql);

    $dados = $res->fetch();


    if(empty($dados)){
        echo "<script>alert('Seu login falhou. LOGIN: [email protected] SENHA: 123');</script>";
    } else {
        $_SESSION['login'] = true;
        $_SESSION['id'] = $dados['id'];
        $_SESSION['name'] = $dados['name'];
        header("location: index.php");
    }
}
    
asked by anonymous 11.10.2017 / 18:21

1 answer

1

The code does not work because no PDOStatement is processed. prepare() returns one, so it is necessary to assign to a variable. To send the query to the it is mandatory to call execute() and finally call fetch()/fetchAll()

change:

$res->prepare($sql);
$res->exec($sql);

By:

$sql = "SELECT idlog, logname FROM login
        WHERE logemail= ? AND logpassword=md5(?)
        AND logBlocked = 'no' AND logstatus = 1;";

$stmt = $res->prepare($sql);
if(!$stmt->execute(array($email, $senha))){
   echo '<pre> erro: ';
   print_r($stmt->errorInfo());
   exit;
}
$dados = $stmt->fetch();
    
11.10.2017 / 19:13