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

怪我咯
Release: 2023-03-12 15:54:01
Original
3749 people have browsed it

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

The example of 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 default from php5 >=5.3.0 To support sqlite3

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

Default methodInterface

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

Use When operating the database with PHP, 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
Copy after login

If pdo is not used, even if you turn on the above parameters, you are still using sqlite2. If you don’t believe me, please 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>
<?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); ?>
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!