Home >Backend Development >PHP Tutorial >Configuration example of php using pdo to connect to sqlite3, pdosqlite3_PHP tutorial

Configuration example of php using pdo to connect to sqlite3, pdosqlite3_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:50:501151browse

Configuration example of php using pdo to connect to sqlite3, pdosqlite3

This article describes the configuration method of php using pdo to connect to sqlite3. Share it with everyone for your reference, the details are as follows:

When I first started using php sqlite, I always thought that I was using sqlite3. In fact, it was not the case. PHP only started to support sqlite3 by default from php5 >=5.3.0

Please refer to the official document http://www.php.net/manual/zh/sqlite3.open.php

Default method interface:

public void SQLite3::open ( string $filename [, int $flags = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE [, string $encryption_key ]] )

When using PHP to operate the database, I found that PHP only supports Sqlite2 by default and does not support the latest version of Sqlite3. If you want to support Sqlite3, you must use PDO. To use PDO, you need to load the two modules php_pdo.dll and php_pdo_sqlite.dll in php.ini. As follows:

extension=php_pdo.dll
extension=php_pdo_sqlite.dll

If pdo is not used, even if you turn on the above parameters, you are still using sqlite2. If you don’t believe me, you can visit the generated database and see if there is a prompt at the beginning of the file:

** This file contains an SQLite 2.1 database **

When the php environment does not enable the above supported configuration, the following error will be reported:

Fatal error: Call to undefined function sqlite_open()

sqlite3 example:

<html>
<&#63;php
//$dsn = 'sqlite:sql.db';
try
{
//$dbh = new PDO($dsn, $user, $password);  //建立连接
// $dbh = new PDO('sqlite:yourdatabase.db');
$dbh = new PDO('sqlite:itlife365.com');
echo 'Create Db ok' ;
//建表
$dbh->exec("CREATE TABLE itlife365(id integer,name varchar(255))");
echo 'Create Table itlife365 ok<BR>';
$dbh->exec("INSERT INTO itlife365 values(1,'itlife365.com')");
echo 'Insert Data ok<BR>';
$dbh->beginTransaction();
$sth = $dbh->prepare('SELECT * FROM itlife365');
$sth->execute();
//获取结果
$result = $sth->fetchAll();
print_r($result);
$dsn=null;
}
catch (PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
$dsn = null;
}
&#63;>
</html>
<&#63;php $dbh = null;//或使用unset($dbh); &#63;>

Verification: View database:

Displayed in the file header:

SQLite format 3***

For more instructions, please refer to the official website: http://cn.php.net/manual/zh/ref.pdo-sqlite.php

Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP database operation skills based on pdo", "php Oracle database programming skills summary", "PHP MongoDB database operation skills collection", "php "Introduction Tutorial on Object-Oriented Programming", "Summary of PHP String Usage", "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"

I hope this article will be helpful to everyone in PHP programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1133063.htmlTechArticleConfiguration example of php using pdo to connect to sqlite3, pdosqlite3 This article describes the configuration method of php using pdo to connect to sqlite3. Share it with everyone for your reference, the details are as follows: I just started using it...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn