SQLite Tutorial in PHP: First Steps
Using SQLite will begin with installing it and creating a database. Installation is only required on your own server or local computer. To use SQLite on mutual hosting, let's go straight to the check/create base step.
Its installation is simple, and the best way to verify its correctness is to create a new base, as in the script below .
Why use SQLite
You can choose to use SQLite or MySQL on your website.
Advantages of SQLite:
- It does not require a MySQL database on the server, you use your own storage, an SQLite file.
- We can save the database by simply downloading the file!
- It can be used locally with classic programs written in C or PHP.
- It can be used for standalone web applications in HTML 5.
In return:
- Access to SQLite databases can slow down if the size becomes large.
- Multiple users cannot edit the database at the same time.
- The extension is not enabled for all shared hosts.
Install SQLite for PHP
The SQLite library is not enabled by default, you need to change the PHP.INI file in the PHP directory and include two lines by deleting the semicolon in the prefix:
extension=php_pdo_sqlite.dll
extension=php_sqlite3.dll
You can make SQLite work in PHP with XAMPP.
To check if SQLite works, place the sql lite-check.php script (it is in the archive) in the Wamp www subdirectory and run it in localhost.
Or upload it to the server and run the page, for example: https://www.iqlevsha.ru/sqlite-check.php
Script for SQLite 3 (starting with PHP 5.4):
<?php
$dbname='base';
if(!class_exists('SQLite3'))
die("SQLite 3 NOT supported.");
$base=new SQLite3($dbname, 0666);
echo "SQLite 3 supported.";
?>
Script for SQLite 2:
<?php
$dbname='base';
$base=new SQLiteDatabase($dbname, 0666, $err);
if ($err)
die("SQLite NOT supported.");
echo "SQLite supported.";
?>
This code creates a database named base. If the extension is not available, $ base is set to false.
If this works, the base file will appear in the script directory.
Loading
- Full SQLite 2 source code in ZIP archive and SQLite 3 source code.