1

I have been trying to create a database in SQLite in PHP like this:

$db = new SQLite3("users.db");

– but I got this error:

Fatal error: Class 'SQLite3' not found

– although, this works:

if ($db = sqlite_open('d', 0666, $e)) { 
    sqlite_query($db, 'CREATE TABLE foo (bar varchar(10))');
    sqlite_query($db, "INSERT INTO foo VALUES ('fnord')");
    $result = sqlite_query($db, 'select bar from foo');
    var_dump(sqlite_fetch_array($result)); 
    echo($result);
} else {
    die($sqliteerror);
}

– what's the difference? I would rather use the class, but why can't I?

Luke
  • 2,038
  • 3
  • 22
  • 46

1 Answers1

0

Maybe you have a PHP < 5.3?

SQLite functions, as per comments in official docs:

applies only to sqlite databases for sqlite version 2 and below. These cannot be used on sqlite3 databases.

SQLite3 is available for PHP >= 5.3 and — obviously — works with SQLite 3.

Please note that you can also use PDO class to interact with a SQLite database and, in my opinion, this is the best solution.

fusion3k
  • 11,568
  • 4
  • 25
  • 47
  • Link for that quote please? – Luke Feb 25 '16 at 03:09
  • I'm not sure if this matters, but this comment has **-6** votes on the official documentation. Should we still trust it? – Luke Feb 25 '16 at 21:00
  • I don't know. [Here](http://stackoverflow.com/questions/16728265/how-do-i-connect-to-an-sqlite-database-with-php) some problems reported. You have to try to open a sqlite3 dbfile to see the behavior. BTW, why not PDO? – fusion3k Feb 25 '16 at 21:18
  • I'll think about PDO. I really don't know how to use it, but I can probably figure it out. Just wondering exactly why it is better... – Luke Feb 25 '16 at 21:20
  • Sure not sqlite. It is removed since php 5.4, so when you will update php (or copy yr code on another server) you will have to update the code. – fusion3k Feb 25 '16 at 21:24
  • Okay. "in my opinion, this is the best solution" why? – Luke Feb 25 '16 at 21:50
  • because is multi-driver: at the moment you can use it with 12 different db structure, it is robust, it is widespread, so you can find easily documentation, tutorials and help. In your case, I think it is the only solution, because is never a smart choice to start using a dead class/command. – fusion3k Feb 25 '16 at 21:56