doudoulb1234 2014-03-11 03: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 11: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部