How to connect to a SQLite3 db with PHP

2019-01-17 19:50发布

问题:

I'm new to SQLite3 and PHP and was wondering whether and how I could connect to a SQLite3 database with PHP.

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.

回答1:

<?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



回答2:

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



回答3:

This is the best way I have found and I got it directly from the sqlite website:

<?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:

$results = $db->query('select * from db');

?>



<?php  while ($row = $results2->fetchArray()) {?> 
<td><h4><?php echo $row['id'];?></h4></td>
<?php } ?>


标签: php sqlite3