简体   繁体   English

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

[英]Can't access the SQLite database with MAMP and PHP

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. 事实上,我已经成功创建了一个MySQL数据库并与PHP进行了交互。

My problem is I can't seem to access a SQLite database file with it. 我的问题是我似乎无法用它访问SQLite数据库文件。 I am using MAMP to host locally for now. 我现在正在使用MAMP在本地托管。 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);
?>

As you have access to the database (your code doesn't die ) , I'd say there's got to be an error later ;-) 由于您可以访问数据库(您的代码不会die ,我会说以后会出现错误;-)

Looking at your SQL query, I see this : 看看你的SQL查询,我看到了这个:

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 will only fetch the first row of your data -- which means you might need to use sqlite_fetch_array or sqlite_fetch_object if you want to access all the fields of your resulset. 此外,请注意sqlite_fetch_single只会获取数据的第一行-这意味着你可能需要使用sqlite_fetch_arraysqlite_fetch_object如果你想访问你resulset的各个领域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM