doutuo6048 2016-10-20 16:17
浏览 55
已采纳

在使用PDO创建SQLite数据库时,为什么要使用__DIR__?

I am very junior working with PHP, and until now, I have been working with mysqli. I have been told to learn PHP in general, so, I found a tutorial for doing CRUD using SQLite PDO.

I literally just started it with the first video, and I am confused already... I have done research on internet and php documentation, but I can't find the answer I am looking for.

$db = new PDO("sqlite:".__DIR__."/the_database.db");

This is the line to create the database; my question:

Does the database needs to be in the root directory of my project? I am finding so many different options on internet..... almost all of them the database doesn't even finish with .db but with .sqlite .... but I hope this part will be explained later on the tutorial.

My big question is, why am I adding ".__DIR.__", is it because it HAS TO BE on the root folder? Or could I just place it as $db = new PDO("sqlite:"the_database.db"); ? I have been trying to answer the question myself, trying to find how to "SHOW DATABASES" or something like that but this SQLite seems so complex....

Thank you

  • 写回答

1条回答 默认 最新

  • duanguangsong2380 2016-10-20 16:34
    关注

    First of all, you don't have to use PDO with SQLite. You can use it with mysql if you wish. And I am sure that there are a lot of videos that are using mysql as well. Though I am strongly convinced that all video tutorials are crap that will make you not a programmer but something opposite.

    Now back to SQLite. So your question boils down to the __DIR__ constant which simply denotes the directory where lies the script where it written.

    You see, SQLite database is simply a file. So it can have whatever name or extension, and be placed in any directory of your choice. The only problem with is the access to a newly created database. Imagine there are two php scripts on your site, one is

    /index.php
    

    and another is

    /account/register.php
    

    both working with a database.

    If you address an SQLite database file as just "sqlite:the_database.db", then you'll end up with TWO databases - one in the root folder and one in the account folder. This is why you have to always prefix the filename with the absolute path. And __DIR__ constant is one of the ways to do so. If have a single file where $db = new PDO is called, then it's ok to use this constant

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型