dou4624 2017-02-26 03:30
浏览 66
已采纳

将SQLite与PHP代码连接起来

I created a database with one table using SQLite. However, I am trying to get the data using PHP code but it doesn't work. Any help?

My code:

if ($pdo == null) {
    $pdo = new PDO('sqlite:/db/attendance');
}

$result = $myPDO->query("SELECT * from Student");

enter image description here

  • 写回答

1条回答 默认 最新

  • drhib62644 2017-02-26 04:20
    关注

    When you ask PHP to open /db/attendance you are specifying an absolute path: a file called attendance in a directory called db in the root of your filesystem.

    You probably want to specify either a relative path, or an absolute path that is built from your script's location.

    Try leaving the leading / off of the front of your path:

    $pdo = new PDO('sqlite:db/attendance');
    

    or, even better, building it from the __DIR__ magic constant:

    $pdo = new PDO('sqlite:' . __DIR__ . '/db/attendance');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?