<p>I've been trying to use SQLite with the PDO wrapper in PHP with mixed success. I can read from the database fine, but none of my updates are being committed to the database when I view the page in the browser. Curiously, running the script from my shell does update the database. I suspected file permissions as the culprit, but even with the database providing full access (chmod 777) the problem persists. Should I try changing the file owner? If so, what to?</p>

<p>By the way, my machine is the standard Mac OS X Leopard install with PHP activated.</p>

<p>@Tom Martin</p>

<p>Thank you for your reply. I just ran your code and it looks like PHP runs as user _www. I then tried chowning the database to be owned by _www, but that didn't work either.</p>

<p>I should also note that PDO's errorInfo function doesn't indicate an error took place. Could this be a setting with PDO somehow opening the database for read-only? I've heard that SQLite performs write locks on the entire file. Is it possible that the database is locked by something else preventing the write?</p>

<p>I've decided to include the code in question. This is going to be more or less a port of <a href="https://stackoverflow.com/questions/6936/using-what-ive-learned-from-stackoverflow-html-scraper">Grant's script</a> to PHP. So far it's just the Questions section:</p>

<pre><code>&lt;?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"&gt;.*?([,\d]+)&lt;\/div&gt;.*?Reputation/s', $page, $rep);
$rep = preg_replace("/,/", "", $rep[1]);

preg_match('/iv class="summarycount".{10,60} (\d+)&lt;\/d.{10,140}Badges/s', $page, $badge);
$badge = $badge[1];

$qreg = '/question-summary narrow.*?vote-count-post"&gt;&lt;strong.*?&gt;(-?\d*).*?\/questions\/(\d*).*?&gt;(.*?)&lt;\/a&gt;/s';
preg_match_all($qreg, $page, $questions, PREG_SET_ORDER);

$areg = '/(answer-summary"&gt;&lt;a href="\/questions\/(\d*).*?votes.*?&gt;(-?\d+).*?href.*?&gt;(.*?)&lt;.a)/s';
preg_match_all($areg, $page, $answers, PREG_SET_ORDER);

echo "&lt;h3&gt;Questions:&lt;/h3&gt;\n";
echo "&lt;table cellpadding=\"3\"&gt;\n";

foreach ($questions as $q)
{
    $query = 'SELECT count(id), votes FROM Questions WHERE id = '.$q[2].' AND type=0;';
    $dbitem = $db-&gt;query($query)-&gt;fetch(PDO::FETCH_ASSOC);
    if ($dbitem['count(id)'] &gt; 0)
    {
        $lastQ = $q[1] - $dbitem['votes'];
        if ($lastQ == 0)
        {
            $lastQ = "";
        }
        $query = "UPDATE Questions SET votes = '$q[1]' WHERE id = '$q[2]'";
        $db-&gt;exec($query);
    }
    else
    {
        $query = "INSERT INTO Questions VALUES('$q[3]', '$q[1]', 0, '$q[2]')";
        echo "$query\n";
        $db-&gt;exec($query);
        $lastQ = "(NEW)";
    }
    echo "&lt;tr&gt;&lt;td&gt;$lastQ&lt;/td&gt;&lt;td align=\"right\"&gt;$q[1]&lt;/td&gt;&lt;td&gt;$q[3]&lt;/td&gt;&lt;/tr&gt;\n";
}

echo "&lt;/table&gt;";

?&gt;
</code></pre>

[{'answer_id': 61102, 'author': 'Tom Martin', 'author_id': 5303, 'author_profile': 'https://Stackoverflow.com/users/5303', 'pm_score': 1, 'selected': False, 'text': '<p>I think PHP commonly runs as the user "nodody".  Not sure about on Mac though.  If Mac has whoami you could try <code>echo exec(\'whoami\');</code> to find out.</p>\n'}, {'answer_id': 61243, 'author': 'Michał Niedźwiedzki', 'author_id': 2169, 'author_profile': 'https://Stackoverflow.com/users/2169', 'pm_score': 5, 'selected': True, 'text': "<p>Kyle, in order for PDO/Sqlite to work you need write permission to directory where your database resides.</p>\n\n<p>Also, I see you perform multiple selects in loop. This may be ok if you are building something small and not heavy loaded. Otherwise I'd suggest building single query that returns multiple rows and process them in separate loop.</p>\n"}, {'answer_id': 1352234, 'author': 'Karl Blessing', 'author_id': 165436, 'author_profile': 'https://Stackoverflow.com/users/165436', 'pm_score': 0, 'selected': False, 'text': "<p>@Tom\nDepends on how the hosting is setup, If the server runs PHP as an Apache Module then its likely that it is 'nobody' (usually whatever user apache is setup as). But if PHP is setup as cgi (such as fast-cgi) and the server runs SuExec then php runs as the same user who owns the files. </p>\n\n<p>Eitherway the folder that will contain the database must be writable by the script, either by being the same user, or by having write permission set to the php user. </p>\n\n<p>@Michal\nThat aside, one could use beginTransaction(); perform all the actions needed then comit(); to actually comit them. </p>\n"}, {'answer_id': 3470364, 'author': 'paolo_O', 'author_id': 418707, 'author_profile': 'https://Stackoverflow.com/users/418707', 'pm_score': 0, 'selected': False, 'text': "<p>Well, I had the same problem now and figured it out by a mistake: just put every inserting piece of SQL instruction inside a <code>try...catch</code> block that it goes. It makes you do it right way otherwise it doesn't work. Well, it works now. Good luck for anyone else with this problem(as I used this thread myself to try to solve my problem).</p>\n"}, {'answer_id': 3695324, 'author': 'user445622', 'author_id': 445622, 'author_profile': 'https://Stackoverflow.com/users/445622', 'pm_score': 1, 'selected': False, 'text': '<p>For those who have encountered read-only issues with SQLite on OS X:</p>\n\n<p>1) Determine the Apache <strong>httpd</strong> user and group the user belongs to: </p>\n\n<blockquote>\n  <p>grep "^User" /private/etc/apache2/httpd.conf<br>\n   groups _www</p>\n</blockquote>\n\n<p>2) Create a subdirectory in <strong>/Library/WebServer/Documents</strong> for your database(s) and change the group to the httpd\'s group:</p>\n\n<blockquote>\n  <p>sudo chgrp _www /Library/WebServer/Documents/db</p>\n</blockquote>\n\n<p>A less secure option is to open permissions on <strong>/Library/WebServer/Documents</strong>:</p>\n\n<blockquote>\n  <p>sudo chmod a+w /Library/WebServer/Documents</p>\n</blockquote>\n'}, {'answer_id': 4280229, 'author': 'Attilio', 'author_id': 520558, 'author_profile': 'https://Stackoverflow.com/users/520558', 'pm_score': 2, 'selected': False, 'text': '<p>I found the answer on the <a href="http://www.php.net/manual/en/ref.pdo-sqlite.php#57356" rel="nofollow">PHP manual</a> "The folder that houses the database file must be writeable."</p>\n'}]
['https://Stackoverflow.com/questions/61085', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/658/']