2

I want to try the JSON1 extension for SQLite in PHP7 but it is not enabled by default.

So I have compiled a new php_sqlite3.dll with the JSON1 extension enabled as per these instructions but when I try to start PHP I get an error;

Warning: PHP Startup: Invalid library (maybe not a PHP library) 'sqlite3'
in Unknown on line 0

What am I doing wrong?


Further info

After replacing php_sqlite3.dll with my version, the SQLite3 class no longer works in PHP eg. this line of PHP code;

$cn = new SQLite3(':memory:');

gives this error;

Fatal error: Uncaught Error: Class 'SQLite3' not found

I compiled the new php_sqlite3.dll by doing;

gcc -g -shared -DSQLITE_ENABLE_JSON1 sqlite3.c -o php_sqlite3.dll

Then I replaced php_sqlite3.dll in my Windows PHP /ext folder with the one created above.

Nigel Alderton
  • 2,265
  • 2
  • 24
  • 55

1 Answers1

2

The php_sqlite3.dll library is a library written in php that wraps the SQLite database library.

To use the JSON1 SQLite extension, you need to compile the extension as a stand-alone loadable library, and use the load_extension() SQL function to load the extension at runtime.

$db = new SQLite3('mysqlitedb.db');
$db->exec('load_extension('json1.dll');');

or, you can just call it from php like this:

$db = new SQLite3('mysqlitedb.db');
$db->loadExtension('json1.dll');

It appears that you already know how to compile a C source file, so I won't go into that.

EDIT My mistake.

To compile the json extension , use

gcc -g -shared json1.c sqlite3ext.h -o json1.dll

and make sure that both the json1.c and sqlite3ext.h files are in the same directory. I'm assuming from your example that you are using a Windows compatible GNU compiler (MinGW, or something).

Mark Benningfield
  • 2,800
  • 9
  • 31
  • 31
  • So I don't need to replace `php_sqlite3.dll`? I should instead create a DLL containing only the JSON1 extension and then load that at PHP runtime by adding it to `php.ini`? – Nigel Alderton Feb 09 '18 at 23:18
  • No, you simply call the `sqlite3::loadExtension` [link here](http://php.net/manual/en/sqlite3.loadextension.php) function in php. – Mark Benningfield Feb 09 '18 at 23:24
  • I'm struggling to work out how to make `json1.dll`. I've found a `json1.c` on the SQLite website but when I try to compile it by doing `gcc -shared json1.c -o json1.dll` I get the error `json1.c:26:24: fatal error: sqlite3ext.h: No file or directory`. – Nigel Alderton Feb 10 '18 at 00:42
  • Very well, I'll edit to show the compilation. However, you show that you are using gcc to compile, but you are creating a `.dll` file, not a `.so` file. Are you trying to do this for Linux or Windows? – Mark Benningfield Feb 10 '18 at 01:07
  • I'm using Windows. – Nigel Alderton Feb 10 '18 at 01:14
  • Now `gcc` succeeds but when I do `$db = new SQLite3('mysqlitedb.db'); $db->loadExtension('json1.dll');` it says `Warning: SQLite3::loadExtension(): SQLite Extension are disabled in C:\root\2018-02-07-sqlite_json1\json1.php on line 4`. – Nigel Alderton Feb 10 '18 at 01:44
  • Yes I'm using MinGW. – Nigel Alderton Feb 10 '18 at 01:56
  • Try specifying the `sqlite3.extension_dir` in php.ini, and placing the extension dll in that directory. Failing that, your build of `php_sqlite3.dll` doesn't support SQLite extension loading. – Mark Benningfield Feb 10 '18 at 01:59
  • Different error now. I added `sqlite3.extension_dir = "ext"` to `php.ini` and now the error message is `Warning: SQLite3::loadExtension(): Unable to load extension at 'ext\json1.dll' in C:\root\2018-02-07-sqlite_json1\json1.php on line 4` – Nigel Alderton Feb 10 '18 at 02:54
  • Ok, damn. But thanks anyway for your patience and all your help. – Nigel Alderton Feb 10 '18 at 12:14