简体   繁体   English

使用PDO对象进行Select查询

[英]using PDO object for a Select query

I am a beginner in PDO. 我是PDO的初学者。 I am following a tutorial to learning PDO. 我正在按照教程学习PDO。 I want to use a simple select statement to fetch id of users. 我想使用一个简单的select语句来获取用户ID。

but when I run index.php it dont show any echo ! 但是当我运行index.php它不显示任何echo where is my wrong ? 我哪里错了?

I have four files : 我有四个文件:

config => setting username and password... config =>设置用户名和密码...

DB_Connect : 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 : 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 的index.php

<?php

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



?>

db_connect 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 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. 您没有数据库连接,因为您现在正在将PDO连接分配给变量。 So you're connection is not accessible to the rest of your script. 因此,其余脚本无法访问您的连接。 At least, that's what I'm thinking at the moment. 至少,这就是我目前的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM