PDO查询未执行

时间:2014-11-12 00:43:04

标签: php mysql pdo

下面是我的数据库类的升技。检查功能不会执行。我有var转储一切,它看起来正确,但当我调用结果方法。我没有得到任何数据。请帮忙

Class Database
    {
    private static $_instance = null;
    private $_pdo, 
    $_error = false, //store errors to see if the query as fail or not
    $_query, //last query executed
    $_results, // store result set
    $_count = 0, //count how many results
    $_lastinsertID; // last insert ID

    // method to insert database
    // constructor
    private function __construct()
        {
        try
            {
                //connect to database
            $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)
            {
            echo 'We\'re sorry but there was an error while trying to connect to the database';
            file_put_contents('PDOErrors.txt', $e->getMessage() , FILE_APPEND); // write some details to the PDOError file
            }
        }

    public function check($sql, $params = array()) {

        if(count($params)) {
            $this->_query = $this->_pdo->prepare($sql);

            foreach ($params as $param => $value) { 
                $this->_query->bindParam($param + 1, $value); 

            }

            if($this->_query->execute($params)) {

                //get the results
                $this->_results = $this->_query->fetchAll();

                //store the number of rows
                    $this->_count = $this->_query->rowCount();


               return $this;
            }

    }

        return false;
}

//get results from query
    public function results() {
        return $this->_results;
    }

//return count the result set
    public function count() {
        return $this->_count;
    }

}

这是检查方法打算运行的方式

$check= Database::getInstance()->check('SELECT email, hash FROM users WHERE email = ? AND hash = ? AND status = ?', array('myemail@something.com', '1f3870be27derfc49b3e31a0c6728957f', 0));

当我var_dump($check)我得到了bool true

但是当我这样做时

if($check->count()) {
    echo 'hello i am counting';
} else {
   echo 'sorry nothing';
}

当我运行if if语句时我得到"对不起什么"。这表明查询尚未执行。所以当我做的时候

if($check->results()) {
  echo "we have a result";
}

我什么都没得到。上面的查询应该返回1个结果,但在这种情况下它不会。请帮忙。

1 个答案:

答案 0 :(得分:0)

嗯,既然你正在使用?作为占位符,你可能想放弃绑定params的foreach循环,因为PDO已经在execute方法中自动完成了它。你可以在执行条件中放入一个else,你可以转储PDO errorInfo()来帮助你。

class Database {
    private static $_instance = null;
    private $_pdo,
    $_error = false, //store errors to see if the query as fail or not
    $_query, //last query executed
    $_results, // store result set
    $_count = 0, //count how many results
    $_lastinsertID; // last insert ID

    // method to insert database
    // constructor
    private function __construct() {
        try {
            //connect to database
            $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) {
            echo 'We\'re sorry but there was an error while trying to connect to the database';
            file_put_contents('PDOErrors.txt', $e->getMessage() , FILE_APPEND); // write some details to the PDOError file
        }
    }

    public function check($sql, $params = array()) {
        if(count($params)) {
            $this->_query = $this->_pdo->prepare($sql);

            ## drop this
            //foreach ($params as $param => $value) {
            //    $this->_query->bindParam($param + 1, $value);
            //}

            if($this->_query->execute($params)) {
                //get the results
                $this->_results = $this->_query->fetchAll();

                //store the number of rows
                $this->_count = $this->_query->rowCount();

                return $this;
            }
            // add this to aid you
            else {
                throw new Exception($this->_query->errorInfo());
            }
        }
        return false;
    }

    //get results from query
    public function results() {
        return $this->_results;
    }

    //return count the result set
    public function count() {
        return $this->_count;
    }
}