How to connect SQLite3 database via PDO in PHP5

Source: Internet
Author: User
Tags dsn sqlite database

Using PHP and PDO to configure SQLite database is a very tedious thing, after an afternoon of research, finally cleared the inside of the train of thought. Now take it out and share it with everyone. Beginner, Error please point out.

First we select the Web server software to use, due to compatibility and performance requirements, I chose the most recent version of Apache .

The second step is to use the latest version of the PHP Zip package. Here is a hint, after my painful repeated experiments and review the data, if you want to use the PDO extension to connect to the SQLite database, please do not choose to install packages, because it does not support a lot of PHP extension library files.

Step three, we're going to Http://pecl4win.php.net/ext.php/php_pdo_sqlite.dll and http://pecl4win.php.net/ext.php/php_. Pdo.dll Download PDO two extension library files, the filenames are: Php_pdo.dll and Php_pdo_sqlite.dll. We'll use it later if we save it for a while. By the way, oh, about the driver of Php_pdo, please check the http://pecl4win.php.net/index.php There are detailed information on the installation of different platforms.

The last step, we need to go to the official SQLite website to download the SQLite file package. The address is:

http://www.sqlite.org/download.html Choose the version that suits you. What I'm using is:sqlite_analyzer-3_2_1.zip .

Well, the preparatory work is done, let's get to work! First of all, please follow the above described in the order of installation Apache installation process is very simple , do not repeat . All the way next until finished. But there are some small details that need to be said, and it is these little details that have made me suffer a lot. It is important to note that if Microsoft IIS is installed on the original system, there may be a conflict in the installation, specifically in connection with Port bindings. If you can, please try to uninstall IIS , if you have a way to avoid conflict, you can come up to discuss.

below to start the download down PHP 5.0.4 Zip package extracted, placed under the C:/php folder, can also be placed in their desired place, and finally in the various configuration files inside the change can be. We put the c:/php folder below the Php5ts.dll file under C:/windows/system32, if it is NT or 2000 system, it is placed under the c:/winnt/system32. Then copy the php.ini-recommended file under the C:/php folder to c:/windows/and rename it to PHP.ini. The two PDO bridge driver files that we downloaded (

Php_pdo.dll and Php_pdo_sqlite.dll) put it in C:/php/ext. This folder is a place where PHP is dedicated to the extension library, so you can see a lot of familiar faces ^_^.

Open the php.ini file we just deposited under c:/windows/and start configuring the information. Find the following line.

; Windows Extensions

; Note that ODBC support is built in, and so no DLLs are needed for it.

;

We can find a large number of extension library files behind the two lines, yes, this is where PHP starts loading the extension library, we have to put the PDO extension library we need to load here, let PHP as a module loading, we can use AH ~ ~.

The loading method is simple, add the following two sentences after all the library files:

; Manually added support for driver extensions for PHP PDO

Extension=php_pdo.dll

Extension=php_pdo_sqlite.dll

Here is a very important work, is that we have to the path of the extension library to tell PHP otherwise,PHP does not know where to find these little treasures ^_^, we find the following line: (Find keyword:extension _dir):

; Directory in which the loadable Extensions (modules) reside.

Extension_dir = "C:/php/ext"

Set it to the path of the dynamic inventory side, we are here:c:/php/ext

OK PHP configuration file We have modified, save close.

Let's start right click on the little feather icon in the status bar and select " "opens apache Watch Window, we select the button in the right column Span style= "' font-family:" "Times New Roman"; >stop button to temporarily turn off the service. Then we open "Start - 〉apache HTTP Server 2.0.54- 〉configure Apache server- 〉edit the Apache httpd.conf Configuration File menu, open apache The configuration file for the httpd.conf . Here we are going to put php as apache a module loaded in. We add the following two sentences at the very end of the configuration file. Note the path problem OH:

LoadModule php5_module "C:/php/php5apache2.dll"

AddType application/x-httpd-php. php

This way, when Apache starts, it loads PHP in. And look for the following line

(Find keyword:documentroot):

#

DocumentRoot "D:/website"

Set the root directory of our site. I choose to be under d:/website .

We also look for the following line: (Find keyword:directoryindex)

#

DirectoryIndex index.html Index.html.var

Here we can set our own homepage name , and then add the file name we use . The middle is separated by a space.

For example:

#

DirectoryIndex index.html Index.html.var index.php default.php

At this point, all of our configurations are complete, please save the shutdown. Then launch the Apache Watch Window again, select the "Start" button on the right, start the Apache server,

Last pull ~ ~

If you want to test the SQLite database, we use some sqlite build Table Tools, build a database with some tables, and then enter some data fields, and finally use their favorite editor, in which the following code is entered:


Php

Connect to an ODBC database using driver invocation

$dsn = ' sqlite:c:/sql.db ';

try {

$DBH = new PDO($dsn, $user, $password);

Echo ' PDO Connection Ok ','
‘;

EXEC,$dbh ("CREATE TABLE PKU (id integer,name varchar (255))");

Echo ' Create Table ok ','
‘;

exec, $dbh("INSERT into PKU values (1, ' Jarjin ')");

Echo ' Insert Data ok ','
‘;

$DBH-BeginTransaction();

$sth = $dbhprepare(' SELECT *from PKU ');

$sth, execute();

$result = $sth-Fetchall();

Print_r($result);

$DSN=null;

} catch (pdoexception $e) {

Echo ' Connection failed: '. $e-getMessage();

$DSN=null;

}

?>

(注意:PHP5自身带的SQLite是2版本,不能执行3版本的连接跟操作,POD就是解决这个问题的桥梁,它使PHP5连接并且操作3版本的SQLite,因此,PHP5自身带的SQLite 函数库是无法操作SQLIte3的,必须使用PHP5手册里面PDO的函数来间接驱动SQLite3数据库)

save to our site under the root directory, such as:default.php  we start ie , in the Address bar, enter http://localhost/   enter to see what appears. ^_^

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.