Getting started with php reading sqlite database_PHP tutorial

WBOY
Release: 2016-07-13 10:27:20
Original
1011 people have browsed it

Example of php reading sqlite database, introductory example of operating sqlite in php programming.
Original reference: http://www.jbxue.com/article/php/22383.html
In use Before SQLite, make sure that sqlite and pdo configuration have been enabled in php.ini

Open the PHP.INI file and add the following extension:
extension=php_pdo.dll

extension=php_pdo_sqlite.dll
extension=php_sqlite.dll
The sqlite_open command opens a database file.
Create the file if it does not exist.

sqlite_query can execute SQL statements.
Create a table and insert data.

sqlite_unbuffered_query issues a SELECT statement.
Loop and display the results.

unable to open a temporary database file for storing temporary tables
Unable to open a temporary database file for storing temporary tables. In a Windows environment, if the above error occurs,
please use putenv("TMP= C:/temp");Specify the temporary folder.

Please see the code for details:

<?<span>php
</span><span>//</span><span>临时目录 在Windows环境中,如果出现上述错误,请使用putenv("TMP=C:/temp");指定临时文件夹。
//putenv("TMP=C:/temp");

//打开数据库 www.jbxue.com</span>
<span>if</span> (<span>$db</span> = <span>sqlite_open</span>("test.db",0666,<span>$sqliteerror</span><span>)) {

</span><span>//</span><span>创建表</span>
<span>sqlite_query</span>(<span>$db</span>, "create table user(id integer primary key,name text);"<span>);

</span><span>//</span><span>INSERT语句</span>
<span>$sql</span> = "insert into user values(NULL, '名字')"<span>;

</span><span>//</span><span>执行SQL语句</span>
<span>$res</span> = <span>sqlite_query</span>(<span>$db</span>, <span>$sql</span><span>);

</span><span>//</span><span>SELECT语句</span>
<span>$sql</span> = "select * from user order by id desc limit 20"<span>;

</span><span>//</span><span>执行SQL语句</span>
<span>$res</span> = <span>sqlite_unbuffered_query</span>(<span>$db</span>, <span>$sql</span><span>);

</span><span>//</span><span>显示结果</span>
<span>while</span> (<span>$item</span> = <span>sqlite_fetch_array</span>(<span>$res</span>,<span> SQLITE_ASSOC)) {
</span><span>print</span> "ID:".<span>$item</span>["id"] ."NAME:".<span>$item</span>["name"<span>];
</span><span>print</span> "<BR>"<span>;
};

</span><span>//</span><span>关闭数据库</span>
<span>sqlite_close</span>(<span>$db</span><span>);

} </span><span>else</span><span> {
</span><span>print</span> <span>$sqliteerror</span><span>;
}
</span>?>
Copy after login

PHP+SQLite database operation tutorials and examples

<?<span>php
</span><span>//</span><span>设置脚本最大执行时间</span>
<span>set_time_limit</span>(0<span>);
</span><span>//</span><span>sqlite数据库文件名</span>
<span>$db_name</span> = 'md5.db'<span>;
</span><span>//</span><span>打开sqlite数据库</span>
<span>$db</span> = <span>sqlite_open</span>(<span>$db_name</span><span>);
</span><span>//</span><span>异常处理</span>
<span>if</span>( !<span>$db</span><span> ) {
</span><span>echo</span> '不能连接SQlite文件:',<span>$db_name</span>,'<br />'<span>;
}</span><span>else</span><span>{
</span><span>echo</span> '成功连接SQlite文件:',<span>$db_name</span>,'<br />'<span>;
}
</span><span>//</span><span>创建数据表:MD5密码表</span>
<span>sqlite_query</span>(<span>$db</span>, "CREATE TABLE md5 (s int(4) PRIMARY KEY,d varchar(32))"<span>);
</span><span>//</span><span>插入记录</span>
<span>$s</span> = 0<span>;
</span><span>while</span>(<span>$s</span> <= 999999<span>){
</span><span>$d</span> = <span>md5</span>(<span>$s</span><span>);
</span><span>sqlite_query</span>(<span>$db</span>, "INSERT INTO md5 VALUES (<span>$s</span>,'{<span>$d</span>}')"<span>);
</span><span>$s</span>++<span>;
}
</span><span>//</span><span>检索所有记录</span>
<span>$result</span> = <span>sqlite_query</span>(<span>$db</span>, 'SELECT * FROM md5'<span>);
</span><span>echo</span> '<pre class="brush:php;toolbar:false">'<span>;
</span><span>while</span> (<span>$row</span> = <span>sqlite_fetch_array</span>(<span>$result</span>,<span> SQLITE_BOTH)) {
</span><span>echo</span> 'Md5:',<span>$row</span>['d'],' Src:',<span>$row</span>['s'], '<br />'<span>;
}
</span><span>echo</span> '
'; //关闭SQLite连接 sqlite_close($db); ?>
Copy after login

php reads sqlite entry version

<?<span>php
</span><span>//</span><span>打开sqlite数据库
//$db = @sqlite_open("MM.sqlite", 0666, $error); // 不支持
//$db = new PDO('sqlite:MM.sqlite');
//异常处理</span>
<span>if</span> (!<span>$db</span>) <span>die</span>("Connection Sqlite failed.\n"<span>);
</span><span>//</span><span>添加一个叫做foo的数据库
//@sqlite_query($db, "CREATE TABLE foo (bar varchar(10))");
//插入一条记录
//@sqlite_query($db, "INSERT INTO foo VALUES ('fnord')");
//检索所有记录</span>
<span>$result</span> = <span>$db</span>->query('select BottleEncryptUsrName from BottleTable4'<span>);
</span><span>//</span><span>打印获取的结果</span>
<span>foreach</span>(<span>$result</span> <span>as</span> <span>$row</span><span>){
</span><span>echo</span> <span>$row</span>[0<span>];
</span><span>echo</span> "<br>"<span>;
}
</span>?>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/818150.htmlTechArticleExample of php reading sqlite database, introductory example of operating sqlite in php programming. Original reference: http://www .jbxue.com/article/php/22383.html Before using SQLite, make sure that php.ini has been enabled...
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