Page 1 of 1

how to load DB login info from external file?

Posted: Mon Dec 13, 2010 9:14 pm
by someguyhere
At the top of several pages, I have the following code to connect to a DB:

Code: Select all

$mysqli = new mysqli("localhost", "db_user", "db_password", "db_name");
I'd like to externalize it using require_once or something like that, so that I only have to maintain one file (it will have other configuration settings as well). When I tried require_once, it just displayed the single line from the file and then died.

What is the right/best approach?

file_get_contents()
require_once()
include_once()

Something else?

Re: how to load DB login info from external file?

Posted: Mon Dec 13, 2010 10:44 pm
by califdon
The normal practice is to use require_once(). Be sure that the other file has <?php and ?> tags in it. If you do that and it fails, something else is wrong with your script.

Re: how to load DB login info from external file?

Posted: Tue Dec 14, 2010 7:33 am
by someguyhere
califdon wrote:Be sure that the other file has <?php and ?> tags in it.
That's what I did wrong. I assumed since it was being pulled into a file that already had the opening and closing php tags, it wasn't needed. Bad assumption :)