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

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.

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

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?