I can not create class in php and use connection with PDO

5

I am creating a class that looks for database data to be displayed. For example, one of the functions of the class is to pick the date, from a die, in the bank and display it on the screen. The way I'm doing, it's not working.

Follow my code

<?php

class contaEntrada {

var $dataConta;
var $descricaoConta;
var $valorConta;

function conectar(){
    $host = "localhost";
    $user = "root";
    $pass = "root";
    $dbname = "fluxo_de_caixa";

    try {
        $opcoes = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
        $pdo = new PDO("mysql:host=localhost;dbname=fluxo_de_caixa;", "root", "root", $opcoes);
    } catch (Exception $e) {
        echo $e->getMessage();
    }
    return $pdo;
}

function pegaDataConta(){
    $pdo = conectar();
    $pegaData=$pdo->prepare("SELECT data FROM entrada WHERE id_entrada:id");
    $pegaData->bindValue(":id", 30);
    $pegaData->execute();
}

function mostraConta(){
    echo $this->descricaoConta = "Conta de luz";
}

function valorConta(){
    echo $this->valorConta = "50,00";
}
}

And I'm calling the functions in another file. index.php

<?php
require_once "con/conexao.php";
require_once "classes/contaEntrada.php";

$entrada = new contaEntrada();

$entrada->pegaDataConta();
echo " - ";
$entrada->mostraConta();
echo " - ";
$entrada->valorConta();
    
asked by anonymous 29.10.2015 / 03:29

2 answers

1

If this is your complete code, you may have forgotten to call the function pegaDataConta() , if you are already calling it, below $pegaData->execute() make var_dump($pegaData->fetchAll()); to display all the data on the page

I took your code and changed it to a mysql database of mine and it works perfectly, so try:

<?php

function conectar(){
    $user = 'root';
    $pass = 'root';
    try {
        $opcoes = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
        $pdo = new PDO('mysql:host=localhost;dbname=fluxo_de_caixa', $user, $pass, $opcoes);
    } catch (Exception $e) {
        echo $e->getMessage();
    }
    return $pdo;
}

function pegaDataConta(){
    $pdo = conectar();
    $pegaData =$pdo->prepare("SELECT * FROM entrada");
    $pegaData->execute();
    var_dump($pegaData->fetchAll());
}

pegaDataConta();

If it is another problem let me know that I edit the answer, otherwise this is normal in your code.

    
29.10.2015 / 04:14
1
function pegaDataConta(){
    $pdo = conectar();
    $pegaData=$pdo->prepare("SELECT data FROM entrada WHERE id_entrada:id");
    $pegaData->bindValue(":id", 30);
    $pegaData->execute();
}

Do not forget to put the operation you want, in case it would be the same, one is missing in the query also make the method return the data obtained with fetch() or fetch()

function pegaDataConta(){
    $pdo = conectar();
    $pegaData=$pdo->prepare("SELECT data FROM entrada WHERE id_entrada = :id");
    $pegaData->bindValue(":id", 30);
    if(!$pegaData->execute()){
       print_r($pdo->errorInfo);
    }
    return $pegaData->fetchAll(PDO::FETCH_ASSOC);

Suggestions:

Do not use the var keyword to set a class property in php4 , use some of the access modifiers, public , protected , or private .

It is better to create a class attribute to save the connection, once created it in the builder just call.

    
29.10.2015 / 12:14