如何使用PHP连接到SQLite数据库?
问题描述:
我有一个SQLite数据库,正在尝试用PHP连接到它.这就是我正在使用的:
I have an SQLite database and am trying to connect to it with PHP. This is what I'm using:
<?php
$dbconn = sqlite_open('combadd.sqlite');
if ($dbconn) {
$result = sqlite_query($dbconn, "SELECT * FROM combo_calcs WHERE options='easy'");
var_dump(sqlite_fetch_array($result, SQLITE_ASSOC));
} else {
print "Connection to database failed!\n";
}
?>
但是,出现此错误:
警告:
sqlite_open()
[function.sqlite-open]:第4行的C:\xampp\htdocs\deepthi\combadd\combadd_db.php
中的文件已加密或不是数据库
连接数据库失败!
Warning:
sqlite_open()
[function.sqlite-open]: file is encrypted or is not a database inC:\xampp\htdocs\deepthi\combadd\combadd_db.php
on line 4
Connection to database failed!
出什么问题了,我该如何解决?
What's wrong and how can I fix it?
答
尝试使用PDO代替sqlite_open:
Try to use PDO instead of sqlite_open:
$dir = 'sqlite:/[YOUR-PATH]/combadd.sqlite';
$dbh = new PDO($dir) or die("cannot open the database");
$query = "SELECT * FROM combo_calcs WHERE options='easy'";
foreach ($dbh->query($query) as $row)
{
echo $row[0];
}
$dbh = null; //This is how you close a PDO connection