1

我最近开始观看这一系列教程 ( http://www.youtube.com/playlist?list=PLfdtiltiRHWF5Rhuk7k4UAU1_yLAZzhWc ) 并在编写 DB 类时遇到了问题。这是第8个视频。

我的查询功能似乎无法正常工作。当显示“已连接”回显时,我可以成功连接到数据库。但是从不显示成功回声..

数据库.php:

<?php
class DB {
    private static $_instance = null;
    private $_pdo, 
            $_query, 
            $_error = false, 
            $_results, 
            $_count = 0;

    private function __construct(){
        try{
            $db = new PDO('mysql:host='.config::get('mysql/host').';dbname='.config::get('mysql/db'), config::get('mysql/username'), config::get('mysql/password'));
            echo '->Connected'.'<br />';
        }
        catch(PDOException $e){
            die($e->getMessage());  
        }
    }

    public static function getInstance() {
        if(!isset(self::$_instance)) {
            self::$_instance = new DB();
        }
        return self::$_instance;
    }

    public function query($sql, $params = array()){
        $this->_error = false;
        if($this->_query = $this->_pdo->prepare($sql)) {
            $x = 1;
            if(count($params)){
                foreach($params as $param){
                    $this->_query->bindValue($x, $param);
                    $x++;
                }
            }
            if($this->_query->execute()){
                echo 'Success';
            }
        }
    }
}

你能发现我的错误吗?试图找到我的函数不起作用的地方,我设法理解查询函数的第一个 if 语句未验证。在 index.php 中,我使用与视频中相同的行:

DB::getInstance()->query('SELECT username FROM users WHERE username = ?', array('alex'));

所有数据库和表都已成功创建。我可以使用经典的 try catch bloc 和查询方法访问和执行 sql 查询。我特别不明白的是查询函数中的第一个if语句条件。

感谢您的时间 !!

4

3 回答 3

1

您的 PDO 对象存储在$db构造函数的局部变量 ( ) 中。而是将其分配给属性 ( $_pdo),因为您的query()方法正在调用$this->_pdo->prepare().

$this->_pdo = new PDO('mysql:host='.config::get('mysql/host').';dbname='.config::get('mysql/db'), config::get('mysql/username'), config::get('mysql/password'));
于 2013-11-01T14:13:30.410 回答
0

__construct() 中的问题

错误:_construct()

正确的是:__construct()

私有函数 __construct() { 尝试 { $this->_pdo = new PDO('mysql:host='.Config::get('mysql/host').';dbname='.Config::get('mysql/ db'), Config::get('mysql/username'), Config::get('mysql/password') ); 回声“连接”;} catch(PDOException $e) { die($e->getMessage()); } }

于 2016-08-29T17:03:56.460 回答
-1

尝试复制/粘贴这个。

    <?php
class DB {
    private static $_instance = null;
    private $_pdo, $_query, $_error = FALSE, $_results, $_Count = 0;

    private function __construct() {
        try {
            $this -> _pdo = new PDO('mysql:host=' . config::get('mysql/host') . ';dbname=' . config::get('mysql/db'), config::get('mysql/username'), config::get('mysql/password'));
            echo "Conectat";
        } catch(PDOExeption $e) {
            die($e -> getMessage());
        }
    }

    public static function getInstance() {
        if(!isset(self::$_instance)) {
            self::$_instance = new DB();
        }
        return self::$_instance;
    }

    public function query($sql, $params = array()){
        $this->_error = false;
        if($this->_query = $this->_pdo->prepare($sql)) {                        
            $x = 1;
            if(count($params)){
                foreach($params as $param){
                    $this->_query->bindValue($x, $param);
                    $x++;
                }            }
            if($this->_query->execute()){
                echo 'Success';
            }
        }
    }

}
于 2015-05-21T17:09:53.630 回答