5

I am trying to use sqlite from PHP. I have the following simple code:

<?php
$db = new SQLiteDatabase("test2.sdb");
unset($db);
?>

As the result of this code (which I execute in the command line "php test2.php") I get:

Fatal error: Class 'SQLiteDatabase' not found in /var/www/test2.php on line 3

Does anybody know how can I make PHP able to use sqlite?

P.S. Here I found out that "SQLite support is enabled by default on a standard Linux PHP compilation starting with PHP 5.0." And I have "PHP Version => 5.2.6-2ubuntu4.6". So, sqlite should be enabled unless the "--disable-sqlite". In my case output of "phpinfo();" does not contain "sqlite" at all.

Roman
  • 2,439
  • 9
  • 32
  • 32

9 Answers9

4

Class SQLiteDatabase is an object from sqlite library, which support was dropped in PHP 5.4, but on various systems and configuration could be disabled in an earlier releases, as this library was long time marked as going to be deprecated.

Library php_sqlite.dll (Windows) or php_sqlite.so (Linux) is no longer supported in newer versions of PHP and was replaced with php_sqlite3.dll or php_sqlite3.so respectively.

You can:

  1. Try to find php_sqlite.dll (php_sqlite.so) somewhere in the Internet. Links like this or this may be helpful for you. However, you'll have to carefully match old SQLite library file to your PHP's platform (x64 or x86), build engine (VC6, VC9 or VC11), version (5.x) and type (TS for thread safe or NTS for non-thread safe). This might be a hard task.

  2. Leave php_sqlite.dll (SQLiteDatabase) behind and ship toward new php_sqlite3.dll (SQLite3 object). You have to first use a tool like SQLite Studio to convert your database file from 2.1 to 3.0 (size can be lowered by even a half) and then carefully compare SQLite and SQLite3 PHP manual pages to change necessary objects and functions call.

If option two, note that this shouldn't be a hard work, as changes aren't that big. For example, what I've learned so far:

  • SQLiteDatabase -> SQLite3,
  • SQLiteDatabase::unbufferedQuery -> SQLite3::query,
  • SQLiteResult::fetchAll(SQLITE_*) -> SQLite3Result::fetchArray(SQLITE3_*) etc.

As for fetching, in old SQLite we had:

$rowsIMEI = $db->unbufferedQuery($imeiSQL)->fetchAll(SQLITE_ASSOC);

foreach($rowsIMEI as $r)
{
    ...
}

While, in new SQLite3 we should:

$rowsIMEI = $db->query($imeiSQL);

while($r = $rowsIMEI->fetchArray(SQLITE3_ASSOC))
{
    ...
}

Other changes requires similar amount of work, so this shouldn't be a life-time process.

I, of course, strongly advice anyone to go forward and choose second option. Progress is in most cases the better one of two available options.

trejder
  • 143
  • 10
  • 1
    Just started with SQLite — found "SQLiteDatabase at first, didn't realise I have to use "SQLite3". (Installing "php5-sqlite" package was a prerequisite.) – Jānis Elmeris Mar 09 '14 at 18:51
2

There's quite a few reports of the same issue out on Google but no definitive solution.

Double-check your SQLlite extension is enabled in PHP, and you should be able to use the sqlite_* family of functions (such as sqlite_open) instead of the OOP approach, not ideal I know.

Also just check that your PECL module for sqlite is >= 1.0.0.

Andy Shellam
  • 1,828
  • 1
  • 12
  • 16
  • How exactly do I check if my SQLite extension is enabled in PHP? I also tried to use "sqlite_open" as the result I got: Fatal error: Call to undefined function sqlite_open() – Roman Feb 28 '10 at 11:04
  • Mock up a simple PHP script with then view that page in your browser - it should have a section for sqlite if it's supported. Have you got the php5-sqlite package installed? If not, install it. If you have, check if you have a extension=sqlite.so (or something similar) line in your php.ini. – Andy Shellam Feb 28 '10 at 11:46
  • I have installed php5-sqlite and problem was resolved. Thank you! – Roman Feb 28 '10 at 14:58
2

I had the same problem.

If you upgraded your php version, some of old configuration files may be referencing a non existent file.

Look at the ".ini" files at "conf.d" directory. In my case the path of my "conf.d" directory is "/etc/php5/conf.d" in a Debian system.

Talking specifically about "SQLiteDatabase" class, my problem was the "sqlite.ini" file pointing to "sqlite.so" which does not exist.

After deleting the "sqlite.ini" file, make sure that there are a ini file pointing to a valid sqlite extension (in this case the "sqlite3.ini" pointing to "sqlite3.so") and restart the apache all worked fine for me.

Hope it helps.

Tatupheba
  • 21
  • 1
1

I had the same problem and I had forgotten to uncomment extension=php_pdo_sqlite.dll

AND

extension=php_sqlite.dll

1

I had a similar problem, and when trying to install sqlite using "yum install sqlite.i386" it said it was installed and up to date, however for some reason there was nothing in my php.ini about it and it wasn't working even though phpinfo was reporting "with sqlite=shared" etc.

Adding "extension=sqlite.so" somewhere in the php.ini made it work, I put mine under the PDO section near the bottom but I doubt it matters where you put it really.

Oh and make sure you restart apache before checking it, or just reboot the box! :)

Kythin
  • 11
  • 1
1

This is an oldie but a goodie, that i have just been stung by AGAIN!!! It turns out that there are sqlite and sqlite3 php extensions to install, you may have the latter but be looking at the manual page for the "sqlite" extension.

My solution was to run:

print_r(get_declared_classes());

And that displays:

.......
[130] => mysqli_stmt
[131] => SQLite3
[132] => SQLite3Stmt
[133] => SQLite3Result
[134] => ImagickException
.......

So my install doesn't define SQLiteDatabase class only SQLite3. Now reference the php manual section for sqlite3.

1

With version PHP 5.6.2 on an Arch Linux system (Current: Nov2014)

Please note also to do to:
Set in /etc/php/php.ini the path where your sqlite3.so extension resides at the section:

[sqlite3]
sqlite3.extension_dir = /path/to/your/modules

Mine are installed at '/usr/lib/php/modules' and can be listed with pacman:

$ sudo pacman -Ql php-sqlite

Which will list the installed sqlite3.so extensions and other files for you.

You can verify if PHP found the extension by querying:

$ php --ri sqlite3

I'm using Adminer at the moment as explained in the wiki.archlinux.org, and it had created a database after I set this path in php.ini.

Thanks for the information provided above and succes.

Ferdi
  • 11
  • 1
0

Check your php.ini (for instance on windows) :

extension=php_pdo.dll
extension=php_pdo_sqlite.dll
extension=php_sqlite.dll
-1

Check your php.ini. 1)Type the path to extensions file manually. Uncoment: extension_dir = "C:\php\ext" 2)Don't forgot to uncoment extensions in "Dynamic Extensions" chapter: extension=php_mysqli.dll extension=php_pdo_sqlite.dll extension=php_sqlite3.dll