无法使用 MAMP 和 PHP 访问 SQLite 数据库

发布于 2024-10-21 19:29:14 字数 541 浏览 0 评论 0原文

我最近一直在学习如何对网站进行编程,现在是时候添加数据库了。事实上,我已经成功创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我老 2024-10-28 19:29:14

由于您可以访问数据库(您的代码不会死亡,我想说稍后一定会出现错误;-)

查看您的 SQL 查询,我看到这个:

SELECT * FROM usernames WHERE username=trevor

您确定不需要在该字符串周围加上引号吗?

像这样:

SELECT * FROM usernames WHERE username='trevor'

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 :

SELECT * FROM usernames WHERE username=trevor

Are you sure you don't need to put quotes arround that string ?

Like this :

SELECT * FROM usernames WHERE username='trevor'

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文