Home  >  Article  >  Backend Development  >  Detailed explanation of the configuration method of php using pdo to connect to sqlite3

Detailed explanation of the configuration method of php using pdo to connect to sqlite3

墨辰丷
墨辰丷Original
2018-06-02 09:51:082425browse

This article mainly introduces the configuration method of php using pdo to connect to sqlite3, and combines the example form with a more detailed analysis of the relevant precautions for php to operate sqlite3 based on pdo. Friends in need can refer to it

Just started When 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 documentation 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 you don’t use pdo, even if you turn on the above parameters, you still use sqlite2. If you don’t believe it, you can visit and see the generated database in the file. Does it prompt at the beginning:

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

Verification: View the database:

is displayed in the file header:

SQLite format 3***

Summary: The above is the entire content of this article , I hope it can be helpful to everyone’s study.

Related recommendations:

PHP method for determining mobile phone devices

PHP strip_tags() strips the strings HTML tag

Detailed explanation of simple digital paging function in PHP

##

The above is the detailed content of Detailed explanation of the configuration method of php using pdo to connect to sqlite3. For more information, please follow other related articles on the PHP Chinese website!

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