Database - SQLite

Introduction

SQLite is a popular free and open-source database management system. It uses a single file to store the entire database.
It is serverless and does not require a separate server process.
Besides MySQL / MariaDB, PHP can also be used to interact with SQLite databases.
Other than the connection string, the SQL commands are similar to MySQL / MariaDB.

Pre-requisite

The directory (folder) where the SQLite database file is stored must have write permission (Can be read and write by PHP).
Permission

Code - Connect to database file


<?php
	// Database file location
	$dbname = "sqlite.db";

	// Create connection
	$conn = new SQLite3($dbname);

	// Check connection
	if (!$conn) {
		die("Connection failed: " . $conn->lastErrorMsg());
	} else {
		echo "Connected successfully";
	}
?>
	

Code - Get data with SQL


<?php
	// Database file location
	$dbname = "sqlite.db";
	
	// Create connection
	$conn = new SQLite3($dbname);
	
	// Check connection
	if (!$conn) {
		die("Connection failed: " . $conn->lastErrorMsg());
	} else {
		// if connected successfully
		$sql = "SELECT * FROM accounts";

		// execute and display all results
		$result = $conn->query($sql);
		while ($row = $result->fetchArray()) {
			print_r($row);
		}
	}	
?>
	

Demo

SQLite Content:
SQLite Content

Result:
Open in new window

Task

Download sqlite.db file.
Create an HTML form to ask for keyword
Search keyword and show results in database table "accounts".

Reference

PHP SQLite: Lightweight SQL Database for PHP