0

I am a beginner in PDO. I am following a tutorial to learning PDO. I want to use a simple select statement to fetch id of users.

but when I run index.php it dont show any echo ! where is my wrong ?

I have four files :

config => setting username and password...

DB_Connect :

class DB_Connect {

    // constructor
    function __construct() {

    }

    // destructor
    function __destruct() {
        // $this->close();
    }

    // Connecting to database
    public function connect() {
        require_once 'include/config.php';
        try {
            $hostname = DB_HOST ;
            $dbname   = DB_DATABASE;
            $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", DB_USER, DB_PASSWORD);
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }

        return $dbh;
    }



}

DB_Functions :

class DB_Functions {

    private $db;

    //put your code here
    // constructor
    function __construct() {
        require_once 'DB_Connect.php';
        // connecting to database
        $this->db = new DB_Connect();
        $this->db->connect();
    }

    // destructor
    function __destruct() {

    }


    function getUsers(){

        $sql = "SELECT * FROM users";
        foreach ($this->$db->query($sql) as $row)
        {
            echo $row->id;
        }

        /*** close the database connection ***/
      //  $db = null;

    }
}

index.php

<?php

    require_once 'include/DB_Functions.php';
    $qr = new DB_Functions();
    $qr->getUsers();



?>
8
  • 1
    $this->$db->query($sql) to $this->db->query($sql) Commented Nov 21, 2014 at 8:36
  • I changed it to db, but I have not any output yet . Commented Nov 21, 2014 at 8:39
  • Do you have connection with your database? Are you fetching your result as an object or an array? try var_Dump($row) Commented Nov 21, 2014 at 8:42
  • Try adding the error_reporting(E_ALL); ini_set('display_errors', '1'); on the top of the page and see what error u get Commented Nov 21, 2014 at 8:42
  • 1
    So its pretty clear that the connection to DB is not established. Debug that connection part and see if all the values uname,pass,host,dbname etc are properly set. Commented Nov 21, 2014 at 8:52

1 Answer 1

1

db_connect

class DB_Connect {
    public $dbh;
    // constructor
    function __construct() {

    }

    // destructor
    function __destruct() {
        // $this->close();
    }

    // Connecting to database
    public function connect() {
        require_once 'include/config.php';
        try {
            $hostname = DB_HOST ;
            $dbname   = DB_DATABASE;
            $this->dbh = new PDO("mysql:host=$hostname;dbname=$dbname", DB_USER, DB_PASSWORD);
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }
}

db_functions

class DB_Functions {

    private $db;

    //put your code here
    // constructor
    function __construct() {
        require_once 'DB_Connect.php';
        // connecting to database
        $this->db = new DB_Connect();
        $this->db->connect();
    }

    // destructor
    function __destruct() {

    }


    function getUsers(){

        $sql = "SELECT * FROM users";
        foreach ($this->db->dbh->query($sql) as $row)
        {
            echo $row->id;
        }

        /*** close the database connection ***/
      //  $db = null;

    }
}

You have no database connection because you're now assigning your PDO connection to a variable. So you're connection is not accessible to the rest of your script. At least, that's what I'm thinking at the moment.

Sign up to request clarification or add additional context in comments.

2 Comments

what is query function ? I must implement query function ?
THe query functions is part of the PDO class. You can't access your PDO class because when you connect to your database using PDO, you create a local variable, which is only accessible in that single function and not in the rest of your class. Therefore you need to make your connection available to the rest of your class like this: $this->dbh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.