Jump to content

[SOLVED] Inlcuding mysql connection info as an include


jaxdevil

Recommended Posts

Hi guys,

 

Its been a bit since i had to ask a question, I have increased the size and dynamics of my program now exponentially, the problem is I inserted the sql connection data (i.e. I copied the data below) on EVERY page, so instead of one single conifg file I have the sql connection settings on each page. I am trying to make it able to switch between databases on the fly and its impossible since the program now has some 87 inter-connected php scripts that connect to the database and it would be best to be able to change the database selected for all  of them with one file, instead of opening 87 pages in notepad and changing the db_name on each. Here is my mysql connection data, what I need to do is make it accessible to each page with some function like...

 

<?php
include(config.inc.php);
?>

 

The only problem is these are also in seperate directories, and the config file would be located in the root public web directory. So how would I format that? i.e. http://www.whatever.com/config.inc.php or /config.inc.php or home/wherever/public_html/config.inc.php etc.

 

Here is my code...

 

<?php
$db_host = "localhost";
$db_user = "xxxxx";
$db_pwd = "xxxxxx";
$db_name = "xxxxx";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?> 

 

Thanks in advance guys.

 

SK

Link to comment
Share on other sites

Thanks guys.

 

I tried those, it does not connect to the database. Here is what I have in the actual config file...

 

<?php
$db_host = "localhost";
$db_user = "xxxxxx";
$db_pwd = "xxxxxx";
$db_name = "xxxxxx";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?> 

 

and here is what I have the include statement as...

 

<?php
include("../config.inc.api.php");
?>

 

 

Now if I replace the include statement with the sql connect data it connects, but when I switch it to the include data it does not connect. I can not figure out what I am doing wrong. These files are in multiple levels also (i.e. the one I am testing on is in public_html/api/old) but I tried using my absolute path to the config.inc.php file in the public_html directory and the unix (../config.inc.api.php) root setting. I have no idea where I am going wrong.

 

Thanks again,

SK

Link to comment
Share on other sites

To form an absolute file system path at runtime, use -

 

require($_SERVER[''DOCUMENT_ROOT''] . "/your_path/yourfile.php");

 

Also, if you require the code and data in the file for your program to work, you should use require() instead of include().

 

Checking your web server log for error will help you troubleshoot why it is not working.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.