如何使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:37:12  来源:igfitidea点击:

How to connect to a SQLite3 db with PHP

phpsqlite

提问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

取自此处:PHP:SQLite3::query - 手册

回答by Dalmas

SQLite is enabled by default with PHP. You have to use the built-in class SQLite3(you will find some examples on this page).

PHP 默认启用 SQLite。您必须使用内置类SQLite3(您将在此页面上找到一些示例)。

回答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 } ?>