如何使用 PHP 连接到 SQLite3 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5041737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect to a SQLite3 db with PHP
提问by kolip
I'm new to SQLite3 and PHP and was wondering whether and how I could connect to a SQLite3 database with PHP.
我是 SQLite3 和 PHP 的新手,想知道是否以及如何使用 PHP 连接到 SQLite3 数据库。
How would I get the data from the db and would it be possible to output them on a browser screen? I've been searching the web for a while now, but couldn't find anything.
我如何从数据库中获取数据,是否可以在浏览器屏幕上输出它们?我已经在网上搜索了一段时间,但找不到任何东西。
Thank you.
谢谢你。
回答by marktucks
<?php
$db = new SQLite3('mysqlitedb.db');
$results = $db->query('SELECT bar FROM foo');
while ($row = $results->fetchArray()) {
var_dump($row);
}
?>
Taken from here: PHP: SQLite3::query - Manual
回答by Dalmas
回答by Adam Falchetta
This is the best way I have found and I got it directly from the sqlite website:
这是我找到的最好的方法,我直接从 sqlite 网站得到它:
<?php
$db = new SQLite3('sqlite3db.db');
$results = $db->query('select * from db');
while ($row = $results->fetchArray()) {
var_dump($row);
}
?>
Also if you're looking to include the php results into html like a table cell or something, go as such:
此外,如果您希望将 php 结果包含到 html 中,如表格单元格或其他内容,请执行以下操作:
$results = $db->query('select * from db');
?>
<?php while ($row = $results2->fetchArray()) {?>
<td><h4><?php echo $row['id'];?></h4></td>
<?php } ?>