Using SQLite in PHP

Here's an example of how to use SQLite in PHP. In this example we use ':memory:' as the filename, which creates the database in memory instead of on the disk. We create a table, insert a couple records, and then select those records.

<?php
 
	$db = sqlite_open(':memory:', 0666, $error);
	$result = sqlite_exec($db, 'CREATE TABLE foo (first TEXT, last TEXT)');
	$result = sqlite_exec($db, "INSERT INTO foo (first, last) VALUES ('Joel', 'Dare')");
	$result = sqlite_exec($db, "INSERT INTO foo (first, last) VALUES ('Heidi', 'Dare')");
	$result = sqlite_array_query($db, "SELECT * FROM foo ORDER BY first, last", SQLITE_ASSOC);
	sqlite_close($db);
 
	print_r($result);
 
?>
comments powered by Disqus
php/using_sqlite_in_php.txt · Last modified: 2020/06/01 22:53 (external edit)