PHP reads the sqlite database entry instance code
- 2020-03-30 03:25:59
- OfStack
Introduction of SQLite
Simply connect to SQLite with PHP to create tables, and use INSERT and SELECT statements to manipulate the SQLite database.
Before using SQLite, we want to make sure that the SQLite and pdo configurations are enabled in php.ini
Open the php.ini file and type the following extensions:
extension=php_pdo.dll
extension=php_pdo_sqlite.dll
extension=php_sqlite.dll
The sqlite_open command is to open a database file.
If no file is created.
Sqlite_query can execute SQL statements.
Create a table and insert the data.
Sqlite_unbuffered_query issues a SELECT statement.
Loop and display the result.
Unable to open a temporary database file for storing temporary tables
Unable to open the temporary database file that stores the temporary tables. In a Windows environment, if the above error occurs,
Please use the putenv (" TMP = C: / temp "); Specify a temporary folder.
Please refer to the code for details:
<?php
//Temporary directory in Windows environment, if the above error, please use putenv("TMP=C:/temp"); Specify a temporary folder.
//putenv("TMP=C:/temp");
//Open database
if ($db = sqlite_open("test.db",0666,$sqliteerror)) {
//Create a table
sqlite_query($db, "create table user(id integer primary key,name text);");
//The INSERT statement
$sql = "insert into user values(NULL, ' The name ')";
//Execute SQL statement
$res = sqlite_query($db, $sql);
//The SELECT statement
$sql = "select * from user order by id desc limit 20";
//Execute SQL statement
$res = sqlite_unbuffered_query($db, $sql);
//According to the results
while ($item = sqlite_fetch_array($res, SQLITE_ASSOC)) {
print "ID:".$item["id"] ."NAME:".$item["name"];
print "<BR>";
};
//Close the database
sqlite_close($db);
} else {
print $sqliteerror;
}
?>
PHP+SQLite database operation tutorial with examples
<?php
//Sets the maximum execution time of the script
set_time_limit(0);
//Sqlite database file name
$db_name = 'md5.db';
//Open the sqlite database
$db = sqlite_open($db_name);
//Exception handling
if( !$db ) {
echo ' Unable to connect SQlite File: ',$db_name,'<br />';
}else{
echo ' Successful connection SQlite File: ',$db_name,'<br />';
}
//Create data table: MD5 password table
sqlite_query($db, "CREATE TABLE md5 (s int(4) PRIMARY KEY,d varchar(32))");
//Insert records
$s = 0;
while($s <= 999999){
$d = md5($s);
sqlite_query($db, "INSERT INTO md5 VALUES ($s,'{$d}')");
$s++;
}
//Retrieve all records
$result = sqlite_query($db, 'SELECT * FROM md5');
echo '<pre>';
while ($row = sqlite_fetch_array($result, SQLITE_BOTH)) {
echo 'Md5:',$row['d'],' Src:',$row['s'], '<br />';
}
echo '</pre>';
//Close the SQLite connection
sqlite_close($db);
?>
PHP reads the sqlite starter version
<?php
//Open the sqlite database
//$db = @sqlite_open("MM.sqlite", 0666, $error); // Does not support
//$db = new PDO('sqlite:MM.sqlite');
//Exception handling
if (!$db) die("Connection Sqlite failed.n");
//Add a database called foo
//@sqlite_query($db, "CREATE TABLE foo (bar varchar(10))");
//Insert a record
//@sqlite_query($db, "INSERT INTO foo VALUES ('fnord')");
//Retrieve all records
$result = $db->query('select BottleEncryptUsrName from BottleTable4');
//Print the obtained results
foreach($result as $row){
echo $row[0];
echo "<br>";
}
?>