tags:

views:

221

answers:

3

I'm using PHP to fetch data from a database.

Here is the code:

<?php  
    $db = new SQLiteDatabase("testDB.db");
    $query = $db->query("SELECT * FROM blog ORDER BY blogdate DESC");
    while($entry = $query->fetch(SQLITE_ASSOC)) { //only one blog entry per day
        echo "<a href=\"display.php?date=".$entry['blogdate']."\">".$entry['blogdate']."</a><br>";
    }
?>

But for some reason it doesn't return an entry that I am certain is in the database. The reason I think it's in the db is that I can see the text of the entry when I view the *.db file.

Here are some specific questions that might help me better understand what's going on:

I CREATE a table called blog. I INSERT tuples into blog using the query function as I did with the SELECT calls. When I DELETE tuples using LIKE, are those tuples being deleted from the database, or are they being deleted from the table blog only? If it is the latter case, how do I get SQLite to delete the tuple from the database completely?

Finally, I've observed some odd behavior. A tuple is added to blog with blogdate as "2009-12-1" (which I treat as a string because there's not date or time type in SQLite). When I run the PHP file with the above code, the entry with 2009-12-1 as blogdate does not show up. I ran another PHP page that searches for tuples with blogdate LIKE 2009-12-1, and it showed up in the search results. Only then did the tuple for 2009-12-1 show up when I SELECT *d for it using the PHP above.

A: 

To check your assertion that the data is in the database, I would suggest opening the database in an database browser.

jussij
+1  A: 

Viewing the binary database file is insufficient to show that a record actually exists in a database. Many databases don't bother actually removing the data from the data file, as that would be a waste of time. Instead, they may flag the block as "deleted", and overwrite it later when saving additional data. So, you assertion that the record is in the database because you can see it in the .db file means nothing, you need to open the file in a program designed to browse the contents of the database and see if it shows up.

Donnie
+3  A: 

The text of a record may show up when you view a DB file in a text/hex editor even though it may have been marked as deleted in the database.

To fully remove these deleted records, try compacting the database.

Nick
+1 for politeness about the presence/absence of a record, and explaining why.
Stewbob
Is my question a newbie question or something?
Teef L
No, I can't see what would be wrong with your code, I was just explaining why you would see it in the raw database file but it may not exist in your table.As others suggested, you should get a database browser to see what data you really have in there.
Nick
My newbie question/comment was @Stewbob.But, you are really nice, Nick. Thanks.
Teef L