我一直在尝试将 SQLite 与 PHP 中的 PDO 包装器一起使用,但效果好坏参半。我可以很好地从数据库中读取,但是当我在浏览器中查看页面时,我的更新都没有提交到数据库中。奇怪的是,从我的 shell 运行脚本确实会更新数据库。我怀疑文件权限是罪魁祸首,但即使数据库提供完全访问权限(chmod 777),问题仍然存在。我应该尝试更改文件所有者吗?如果是这样,该怎么办?
顺便说一句,我的机器是标准的 Mac OS X Leopard 安装并激活了 PHP。
@汤姆马丁
感谢你的回复。我刚刚运行了您的代码,看起来 PHP 以用户 _www 运行。然后我尝试将数据库由_www拥有,但这也不起作用。
我还应该注意 PDO 的 errorInfo 函数并不表示发生了错误。这可能是 PDO 的设置以某种方式打开数据库以进行只读吗?我听说 SQLite 对整个文件执行写锁定。数据库是否有可能被阻止写入的其他东西锁定?
我决定包含有问题的代码。这将或多或少将格兰特的脚本移植到 PHP。到目前为止,它只是问题部分:
<?php
$db = new PDO('sqlite:test.db');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://stackoverflow.com/users/658/kyle");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIE, "shhsecret=1293706652");
$page = curl_exec($ch);
preg_match('/summarycount">.*?([,\d]+)<\/div>.*?Reputation/s', $page, $rep);
$rep = preg_replace("/,/", "", $rep[1]);
preg_match('/iv class="summarycount".{10,60} (\d+)<\/d.{10,140}Badges/s', $page, $badge);
$badge = $badge[1];
$qreg = '/question-summary narrow.*?vote-count-post"><strong.*?>(-?\d*).*?\/questions\/(\d*).*?>(.*?)<\/a>/s';
preg_match_all($qreg, $page, $questions, PREG_SET_ORDER);
$areg = '/(answer-summary"><a href="\/questions\/(\d*).*?votes.*?>(-?\d+).*?href.*?>(.*?)<.a)/s';
preg_match_all($areg, $page, $answers, PREG_SET_ORDER);
echo "<h3>Questions:</h3>\n";
echo "<table cellpadding=\"3\">\n";
foreach ($questions as $q)
{
$query = 'SELECT count(id), votes FROM Questions WHERE id = '.$q[2].' AND type=0;';
$dbitem = $db->query($query)->fetch(PDO::FETCH_ASSOC);
if ($dbitem['count(id)'] > 0)
{
$lastQ = $q[1] - $dbitem['votes'];
if ($lastQ == 0)
{
$lastQ = "";
}
$query = "UPDATE Questions SET votes = '$q[1]' WHERE id = '$q[2]'";
$db->exec($query);
}
else
{
$query = "INSERT INTO Questions VALUES('$q[3]', '$q[1]', 0, '$q[2]')";
echo "$query\n";
$db->exec($query);
$lastQ = "(NEW)";
}
echo "<tr><td>$lastQ</td><td align=\"right\">$q[1]</td><td>$q[3]</td></tr>\n";
}
echo "</table>";
?>