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

Asked

Viewed 3,219 times

2

I don’t know why I made a mistake.

Fatal error: Uncaught Error: Call to Undefined method PDO::fetch() in /opt/lampp/htdocs/myschool.com.br/login.php:24 Trace stack: #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");
    }
}
  • You didn’t pass fetch(PDO::FETCH_ASSOC);&#xA;

  • I think the problem is in prepare, fetch parameters are optional, store $res->prepare($sql); in a variable and instancie exec with it

  • change exec for execute

  • Rafael Augusto fetch(PDO::FETCH_ASSOC); it was the first thing I tried and it didn’t work.

  • Everton gave running - Uncaught Error: Call to Undefined method PDO::execute(). I think the error ta after exec ...

  • And Connection::getInstance returns a PDO instance?

  • yes this ta working, it is an instance of Singleton. But this ta working, even because it took a great job to find out how it worked.

  • The error always occurs on line 24 which is the fetch line. This means that the research works and the connection with the bank also works, even because it had wrong the name of a field and there was a warning field name wrong. Then connect with the bank do the select but time to put inside a stick variable..

  • Felipe Duarte also did not work $variable = $res->prepare($sql); $data= $variable->fecth(); and the error in exec. I tried that now ->>> $res->prepare($sql); $stmt->exec($res); $data = $stmt->fetch(); gave the error Call to a Member Function exec() on array

  • Ah and I declared the variable data and stmt as array, I do not know if it is right but the command does not work if the variables are not declared, unknow variable.

Show 5 more comments

1 answer

0


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

change:

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

For:

$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();
  • After I do what Ray suggests. Warning: Pdostatement::execute() expects Parameter 1 to be array, string Given in /opt/lampp/htdocs/myschool.com.br/login.php on line 24 error: Array ( [0] => [1] => [2] => )

  • @Fabianodesouzapereira corrected the answer, see that I changed the SQL and the execute().

  • 100% rray. Thank you very much. Guy who command hard. Or I understand little of PHP . eh eh.

  • @Fabianodesouzapereira exec() and execute() sometimes confuses :). If you are interested in how to best use PDO see this answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.