ninedoors Posted May 16, 2008 Share Posted May 16, 2008 I would like to use one config.php file for all my database connection on my site. My question is, is there a simple syntax that I can use at the top of the pages I need, instead of including the whole path. Right now I have my config.php file in a config folder so the path is config/config.php. When I try to call it from lets say photos/insert/photos.php php tells me it can't find it. The syntax I use is: include 'config/config.php'; I know it's a noobie question but I would like to know because I have been having to put the config file in every folder which kinda negates the point. Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted May 16, 2008 Share Posted May 16, 2008 you could just refer to the base of your website by adding a slash infront of it <?php include("/config/config.php"); ?> the extra slash will go to your website root easy as cake huh Quote Link to comment Share on other sites More sharing options...
ninedoors Posted May 16, 2008 Author Share Posted May 16, 2008 I just tried that and I'm still getting this error: Warning: include(/config/config.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\root\photos\insert\album.php on line 5 Here's the album.php file <? // Get values from form $nametemp=$_POST['album_name']; include '/config/config.php'; $name = mysql_real_escape_string($nametemp); $query = "INSERT INTO gallery_category (category_name) VALUES ('$name')"; mysql_query($query) or die("Query failed due to: ".mysql_error()); mysql_close(); header("Location: ../photos/uploadform.php"); ?> Quote Link to comment Share on other sites More sharing options...
947740 Posted May 16, 2008 Share Posted May 16, 2008 Try parentheses around the file you are including, like in the example given by Dj Kat? Quote Link to comment Share on other sites More sharing options...
ninedoors Posted May 16, 2008 Author Share Posted May 16, 2008 Nope. Didin't work either, still got this error: Warning: include(/config/config.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\root\photos\insert\album.php on line 5 Nick Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted May 16, 2008 Share Posted May 16, 2008 seems like the path was incorrect if this is the location of your config file C:\xampp\htdocs\root\config\config.php" then it should be <?php include ('/root/config/config.php'); ?> your C:\xampp\htdocs\ is the website root Quote Link to comment Share on other sites More sharing options...
ninedoors Posted May 16, 2008 Author Share Posted May 16, 2008 No, just put that in there. I tried putting all folders after the htdocs and failed still. Warning: include(BigC/config/config.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\BigC\photos\insert\album.php on line 5 Nick Quote Link to comment Share on other sites More sharing options...
ninedoors Posted May 16, 2008 Author Share Posted May 16, 2008 My location of my config file is C:\xampp\htdocs\BigC\config\config.php Just so you know the exact path Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted May 16, 2008 Share Posted May 16, 2008 best idea is to define a constant from the root directory so you don't have to worry about a relative path. You should do this for all your includes example <?php define("includes_folder", $_SERVER['DOCUMENT_ROOT']."/configs/"); define("DB_CONFIG", includes_folder."db.php"); define("PHP_FUNC", includes_folder."functions.php"); #then to call them its simply require_once(PHP_FUNC); require_once(DB_CONFIG); ?> for files that are configs or "include once" I like to use require_once because the files are both required and only needed once instead of an include function. Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted May 16, 2008 Share Posted May 16, 2008 its pretty straight forward only thing you needed to do is add the slash as i explained in my first post <?php include("/BigC/config/config.php") ?> Quote Link to comment Share on other sites More sharing options...
ninedoors Posted May 16, 2008 Author Share Posted May 16, 2008 cooldude832 where do I put this? <?php define("includes_folder", $_SERVER['DOCUMENT_ROOT']."/configs/"); define("DB_CONFIG", includes_folder."db.php"); define("PHP_FUNC", includes_folder."functions.php"); ?> What file and where? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted May 16, 2008 Share Posted May 16, 2008 well its an example modify it as you need at throw it into the top of your template so on each page its there. Quote Link to comment Share on other sites More sharing options...
ninedoors Posted May 16, 2008 Author Share Posted May 16, 2008 Ok, thanks, I'll try it out. Quote Link to comment Share on other sites More sharing options...
technotool Posted May 16, 2008 Share Posted May 16, 2008 I use this format for my include files <?php include ("../config/config.php"); ?> let me know if this works for you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.