PHP SQLite instance tutorial

Source: Internet
Author: User

This tutorial will introduce you to the important methods supported by the SQLite API and provide a simple script template that can be used in your development to show you how to use PHP to interact with the SQLite database. This article assumes that you have installed Apache and PHP.
You do not have to install an interactive SQLite program on your system; but to simplify the creation of a series of initial tables required for this tutorial, you should download and install this program. Create an example table for your SQL query by creating A blank text file and using the file name as the following command (List) in the interactive command prompt to execute the binary program:
Sqlite> create table users (id integer primary key, username TEXT, country TEXT );
Sqlite> insert into users VALUES (1, john, IN );
Sqlite> insert into users VALUES (2, joe, UK );
Sqlite> insert into users VALUES (3, diana, US );

Once the table is created, a script template is created using the SQLite method of PHP.
<? Php
// Set access parameters
$ Db = "users. db ";
// Open database file
// Make sure script has read/write permissions!
$ Conn = sqlite_open ($ db) or die ("ERROR: Cannot open database ");
// Create and execute INSERT query
$ SQL = "INSERT INTO users (id, username, country) VALUES (5, pierre, FR )";
Sqlite_query ($ conn, $ SQL) or die ("Error in query execution:". sqlite_error_string (sqlite_last_error ($ conn )));
// Create and execute SELECT query
$ SQL = "SELECT username, country FROM users ";
$ Result = sqlite_query ($ conn, $ SQL) or die ("Error in query execution:". sqlite_error_string (sqlite_last_error ($ conn )));
// Check for returned rows
// Print if available
If (sqlite_num_rows ($ result)> 0 ){
While ($ row = sqlite_fetch_array ($ result )){
Echo $ row [0]. "(". $ row [1]. ")";
}
}
// Close database file
Sqlite_close ($ conn );
?>

When using PHP SQLite extension to execute SQL queries, perform the following four simple steps:
1. Call the sqlite_open () function to initialize the database handle. The database path and file name (Remember, SQLite is based on files, rather than servers like MySQL) are passed to the function as independent variables.

2. Create an SQL query string and run it using the sqlite_query () function. The result object of these methods varies depending on the type of the query and whether it is successful. A successful SELECT query returns a result object, a successful INSERT/UPDATE/DELETE query returns a resource identifier, and an unsuccessful query returns a "pseudo ". The sqlite_error_string () and sqlite_last_error () methods can be used to capture errors and display corresponding error information.

3. for SELECT queries, the result object can be further processed to extract data from it. When the sqlite_fetch_array () function is used in a loop, each record is retrieved as a PHP array. You can access each field of each record by calling the appropriate keys of the array.

4. Call the sqlite_close () function to end the session.
An innovative move of PHP 5.x is to join the SQLite database engine. SQLite is a file-based, fully functional, and portable database engine. It can be used for the vast majority of SQL operations without increasing the client-server communication load. PHP

This SQLite API in 5.x is activated by default, which means you can use SQLite immediately.
I hope this script module will save you some time when you sit down next time and write SQLite connection/interaction routines in PHP. Pleasant programming!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.