I cannot create php class and use PDO connection

Asked

Viewed 491 times

5

I’m creating a class that fetches data from the database for display. For example, one of the functions of the class is to take the date, of a given, in the database and display on the screen. The way I’m doing, it’s not working.

Follows 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();
  • id_entrada = :id and after the run, you could use the method fetch to return the set referring to this query.

2 answers

1

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

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

<?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’s another problem let me know that I edit the answer, otherwise this is all normal in your code.

  • Hi @Gabrielrodrigues, Cara, I’m calling the function in another file, see: require_once "con/conexao.php"; require_once "classes/containEntrada.php"; $input = new contaEntrada(); $input->pegaDataConta(); $input->mostraConta(); $input->valorContact();

  • Then, at the end of the Footprint function you have to return the values, you can make a $take Return->fetchAll(); and then exbir using var_dump($input->take(););

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, is missing one 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:

Don’t use the keyword var it serves to define a class property in the php4, use any 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 constructor just call.

  • I’m calling the Call function in another file. require_once "con/conexao.php"; require_once "classes/accountEntrada.php"; $input = new accountEntrate(); $input->pegaDataConta(); $input->mostra();

  • @Gustavosevero and what’s the problem?

  • Does not show the date that is in the bank.

  • But in the update of the question you do not echo in the result of the method.

  • @Gustavosevero as is now the method pegaDataConta()

  • It looks like this: unction pegaDataConta(){ //echo $this->dataConta = date("d/m/Y"); $Pdo = connect(); $pegaData=$Pdo->prepare("SELECT data FROM input where id_input=:id"); $pegaData->bindValue(":id", 30); if(!$pegaData->execute()){ print_r($Pdo->errorInfo); } Return $pegaData->fetchAll(PDO::FETCH_ASSOC); } because they suggested that I do this to see what comes back, but nothing happens.

  • @Gustavosevero, do it print_r($entrada->pegaDataConta()); see what returns, say even q is an empty array.

  • Dude, I switched in IF: print_r($Pdo->errorInfo); by print_r($input->pegaDataConta(); , and nothing appears yet. ?

  • @Gustavosevero, thus, after creating the object: $entrada = new contaEntrada(); print_r($entrada->pegaDataConta());

  • Ahhhhh, perfect.

  • Look, I did what you said, but now the page error. "Server error 500"

  • At the beginning of scrpt, put, ini_set('display_errors', true); error_reporting(E_ALL); and see what appears, @Gustavosevero

  • I think with your help on the other question, I can handle the boat. For those who need it... [http://answall.com/questions/95424/comort-manipulationdataNewdo-banco-usando-pdo/95426#95426]

Show 9 more comments

Browser other questions tagged

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