doudoulb1234 2014-03-11 11:42
浏览 553
已采纳

PDO:FetchAll返回空数组

I keep receiving an empty array when i try to execute the following code:

$this->DB->prepare('SELECT * FROM Articles WHERE Author = :username AND Timestamp < :timestamp ORDER BY Timestamp DESC LIMIT :limit;');
$this->DB->bind(':username', $username);
$this->DB->bind(':timestamp', $start);
$this->DB->bind(':limit', $numberOfResults, \PDO::PARAM_INT);
$data = $this->DB->execute();

These are the bind, prepare and execute functions:

public function bind($placeholder, $value, $type = \PDO::PARAM_STR) {

    $this->preparedQuery->bindParam($placeholder, $value, $type);
}

public function prepare($query) {

    if(!$this->preparedQuery = $this->connection->prepare($query)) {

        print_r($this->connection->errorInfo());
        die();
    }
}

public function execute() {

    if(!$this->preparedQuery->execute()) {

        print_r($this->preparedQuery->errorInfo());
        die();
    }

    return $this->preparedQuery->fetchAll(\PDO::FETCH_ASSOC);
}

I have also tryed to manually execute the query in MySQL Workbench and it's perfectly working.

Edit:

This is what the query looks like:

SELECT * FROM Articles WHERE Author = "TestUser" AND Timestamp < "9000-12-31 23:59:59" ORDER BY Timestamp DESC LIMIT 10;

Edit 2:

I just found out that if I remove the condition about the Timestamp the query returns values correctly.

  • 写回答

1条回答 默认 最新

  • douzhengzuo7283 2014-03-12 19:06
    关注

    I have finally come to a solution.

    Thanks to Edit 2 i realized that, just like 90% of my errors, this was a stupid one.

    The $start variable was actually empty even if in the code was correctly initialized in the function declaration. This was causing to have an empty string to be passed to the bindParam() function.

    Personal comment: I am an idiot.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?