MySQL / PHP:成功插入返回0大小数组后的fetchAll()

时间:2017-09-30 13:12:55

标签: php mysql

我成功插入(我可以使用myPhpAdmin检查元素是否已成功插入)数据库中的元素,但是当我获取结果以获取最后插入的条目时,我系统地获得0大小的数组。这让我发疯了。显然有一些我看不到的东西,需要帮助,请:

public function addSomething($myValue)
{
    // this works fine ...
    $stmt = $this->pdo->prepare("INSERT INTO `table` (`blabla`) VALUES (?)");
    $stmt->execute(array($myValue));

    // no luck with that
    $result = $stmt->fetch(PDO::FETCH_ASSOC);

    var_dump($result); // always empty

    //return ...;
}

我尝试fetchAll但没有取得更多成功。当我用rowCount打印行数时我得到了1 ???例如:

var_dump(">> " . $result . " " . $stmt->rowCount() . "<<");

给出:

string(7) ">> 1<<"

1 个答案:

答案 0 :(得分:0)

感谢Ravi和Paul ......希望这对其他人有所帮助。

插入不会返回行

所以为了让事情有效,我想你需要做一些事情:

    $stmt = $this->pdo->prepare("SELECT * FROM `table` WHERE row_id=?");
    $stmt->execute(array($this->pdo->lastInsertId()));

    // return the full new inserted comment
    return $stmt->fetch(PDO::FETCH_ASSOC);