PDO help using Mysql

Asked

Viewed 45 times

0

I’m having trouble connecting the database using PDO, use Mysql.

when executing the connection with the bank appear the following error messages:

Fatal error: Uncaught Exception 'Pdoexception' with message ' in C: wamp64 www phpCRUD config Conecta.php on line 11

Pdoexception: in C: wamp64 www phpCRUD config Conecta.php on line 11

I don’t know where I’m going wrong, someone could help me?

Follow the database configuration code:

<?php
    require_once 'conf.php';
    class Conecta extends conf{
        var $pdo;
        function __construct(){
            $pdo = new PDO('mysql:host='.$this->host.':dbname='.$this->db, $this->usuario, $this->senha);
        }
        function login($email, $senha){
            $stmt = $this->PDO->prepare("SELECT * FROM usuarios WHERE email = :email AND senha = :senha");
            $stmt->bindValue(":email",$email);
            $stmt->bindValue(":senha", $senha);

            $exec = $stmt->execute();
            $run = $stmt->fetchAll(PDO::FETCH_ASSOC);
            return self::$run;
        }
    }

conf.php

<?php
    class conf{
        var $host = 'localhost';
        var $usuario = 'root';
        var $senha = '';
        var $db = 'hoo';
    }

1 answer

1

You’re making a little mess with the attribute $pdo and the class PDO

In creating the attribute $pdo, do not use var, use this way:

private $pdo;

Now that you already have your attribute in the class, let’s assign it an instance of the php PDO class, then in the constructor do so:

$this->pdo = new PDO('mysql:host='.$this->host.':dbname='.$this->db, $this->usuario, $this->senha);
//Atributo $pdo recebe uma instância da classe PDO

The prepare in this way:

$stmt = $this->pdo->prepare("SELECT * FROM usuarios WHERE email = :email AND senha = :senha")

In return also use this way, if you want to return an associative array with all the values of the query

return $stmt->fetchAll(PDO::FETCH_ASSOC);
  • That’s what Felipe Nascimento was. Thank you very much for your help!

Browser other questions tagged

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