views:

38

answers:

2

I have installed SQLite on my UNIX system, and am planning on accessing it from PHP. However, the database is created in the directory I initialize it in, and if a script runs in a different directory a new database is created in the same directory.

Is there an option for SQLite (or the PHP wrappers) to create the databases in one location, and have those databases accessible just by name outside of that directory?

Ideally, I'd like to be able to do something like $db=new SQLiteDatabase("db.test"); in any directory and have it reference the same database, if that makes sense.

+2  A: 

In order to always use the same database (which, afterall, is only a file), you'll have to use an absolute path.

Instead of using 'db.test', which is relative to the current directory, you'll have to use something like '/home/user/development/application/db.test'.
Note the / at the beginning of the path, which makes it absolute.


Of course, up to you to adapt this to your needs :-)

A good possibility, to not put a fully hard-coded path in your code, would be to use dirname(__FILE__) in one of your PHP files, to get the full absolute path to the directory containing that file, and build up from there, to get to your database's file.

Pascal MARTIN
Great, thanks. I've got a singleton to handle a db instance and I can stick the absolute path there and use a global variable or something to choose the database.
Brian Ramsay
You're welcome :-) ;; great :-)
Pascal MARTIN
+1  A: 

You should probably store the absolute path somewhere in a config file that provides that data wherever needed. As said previously, you can use dirname(__FILE__) to get the absolute path to the directory in which the containing PHP script resides, so you can work from there.

d11wtq