PHP+SQLite重复插入


PHP + SQLite duplicate inserts

我所做的每一次插入都会发生两次。我认为页面可能会被点击两次,所以我还添加了一个计数器文件,每次加载都会递增。页面只被点击一次,但每次插入都被插入两次。

<?
$x = intval(file_get_contents("./count.x"));
header("Content-type: text/plain");
$db = new SQLite3("/var/www/images/cache.db");
$statement = $db->prepare('insert into `imgCache` (name,type,data) values("test7","bmp","argh);');
$result = $statement->execute();

while ($row = $result->fetchArray(SQLITE3_ASSOC))
{
print_r($row);
}

$x++;
file_put_contents("./count.x",$x);
?>

在运行此脚本之前,数据库中没有"test7"的条目,count.x的内容为"0"。

当我运行这个脚本时,会发生以下情况

  1. count.x包含文本"1"
  2. 现在数据库中有两个test7条目

PHP执行两次查询:一次是检查是否返回了任何行,第二次是实际返回行。

INSERT语句不是查询,也不返回行。删除fetchArray调用。