Home >Backend Development >PHP Tutorial >Detailed explanation of the configuration example of php using pdo to connect to sqlite3
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.dllIf 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 = '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; } ?> </html> <?php $dbh = null;//或使用unset($dbh); ?>
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!