无法使用 MAMP 和 PHP 访问 SQLite 数据库
我最近一直在学习如何对网站进行编程,现在是时候添加数据库了。事实上,我已经成功创建了一个 MySQL 数据库并使用 PHP 与其进行交互。
我的问题是我似乎无法用它访问 SQLite 数据库文件。我现在使用 MAMP 在本地托管。这是我用来访问数据库并查找并打印出存储在其中的值的代码片段。
<?php
$dbhandle = sqlite_open('/Applications/MAMP/db/sqlite/Users');
if ($dbhandle == false) die ('Unable to open database');
$dbquery = "SELECT * FROM usernames WHERE username=trevor";
$dbresult = sqlite_query($dbhandle, $dbquery);
echo sqlite_fetch_single($dbresult);
sqlite_close($dbhandle);
?>
I have been learning how to program websites lately and the time has come for me to add a database. I have in fact already successfully created a MySQL database and interacted with it with PHP.
My problem is I can't seem to access a SQLite database file with it. I am using MAMP to host locally for now. Here is a snippet of the code I am using to access the db and find and print out a value stored on it.
<?php
$dbhandle = sqlite_open('/Applications/MAMP/db/sqlite/Users');
if ($dbhandle == false) die ('Unable to open database');
$dbquery = "SELECT * FROM usernames WHERE username=trevor";
$dbresult = sqlite_query($dbhandle, $dbquery);
echo sqlite_fetch_single($dbresult);
sqlite_close($dbhandle);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您可以访问数据库(您的代码不会
死亡
),我想说稍后一定会出现错误;-)查看您的 SQL 查询,我看到这个:
您确定不需要在该字符串周围加上引号吗?
像这样:
Also, notice that [`sqlite_fetch_single`][1] will only fetch the first row of your data -- which means you might need to use [`sqlite_fetch_array`][2] or [`sqlite_fetch_object`][3] if you want to access all the fields of your resulset.
As you have access to the database (your code doesn't
die
), I'd say there's got to be an error later ;-)Looking at your SQL query, I see this :
Are you sure you don't need to put quotes arround that string ?
Like this :
Also, notice that [`sqlite_fetch_single`][1] will only fetch the first row of your data -- which means you might need to use [`sqlite_fetch_array`][2] or [`sqlite_fetch_object`][3] if you want to access all the fields of your resulset.