我在使用fetch方法时遇到了一些麻烦。代码如下:PDostatement :: fetch返回false
$stmt = $db->prepare("INSERT INTO x (name) VALUES (:username)");
$stmt->bindParam(':username', $username);
$result = $stmt->execute();
if ($result == '1') {
$id = $stmt->fetch();
return $id;
}
执行查询并插入数据,$ result为'1'。但是,当我尝试获取结果时,它返回false。
任何提示?
无法从INSERT查询中获取 –
谢谢。对不起,这个愚蠢的问题。所以我想最好的办法是在插入后做一个SELECT查询。 – 4ndr345
取决于你想要做什么。你想要插入ID?你可以在MySQL中使用'PDO :: lastInsertId'(显然它不可靠,但我从来没有遇到过问题)。那或SELECT'LAST_INSERT_ID()'。 –