Paul Posted February 28, 2013 Share Posted February 28, 2013 Morning all, I have created the following 2 constants in my config file: //Gallery DB for CMSdefine('GalleryMYSQL', 'C:\Users\Paul\Documents\Work\Projects\Work in Progress\Northern Lights (Feb 2013)\testing\g_mysql_con.php');//Prints DB for CMSdefine('PrintsMYSQL', 'C:\Users\Paul\Documents\Work\Projects\Work in Progress\Northern Lights (Feb 2013)\testing\p_mysql_con.php'); Which one is used is dependant on where the user logs in. When they log in a cookie is created that stores either 'Gallery' or 'Prints'. I then have the folowing code to call the correct constant: $connect = $_COOKIE['cms_site'].'MYSQL'; require($connect); When I run the file I get the following error: An error has occurred in script 'C:\xampp\htdocs\NorthernLights\public_html\cms\add_category.php' on line 39:require(GalleryMYSQL) [function.require]: failed to open stream: No such file or directory Date/Time: 2-28-2013 09:39:32 . print_r(Array, 1) . I've tested the path to the constant, it opens the correct file. The error message above even contains the correct name for the constant yet it says it doesn't exist. I'm stumped. Any help would be appreciated. Cheers Paul Link to comment Share on other sites More sharing options...
Larry Posted March 1, 2013 Share Posted March 1, 2013 You might think that solution would work, but the end result is that you're creating a string in $connect whose value happens to be the same as a constant. When you provide this variable to the require() function, you're providing the string value, not the constant. I don't know of a way to dynamically determine the constant name to use and then use that as a constant. Link to comment Share on other sites More sharing options...
Paul Posted March 2, 2013 Author Share Posted March 2, 2013 Larry, Thanks for the reply. I tried a different approach by using the code below: if(isset($_COOKIE['cms_site'])) { if(($_COOKIE['cms_site']) == 'Gallery') { require(GalleryMYSQL); } else { require(PrintsMYSQL); } } else { $url = BASE_URL . 'login.php'; header("Location: $url"); } This worked OK. Rather than use all this code on every page that needs a DB connection I tried wrapping it in a function as follows: function set_con (){ if(isset($_COOKIE['cms_site'])) { if(($_COOKIE['cms_site']) == 'Gallery') { require(GalleryMYSQL); } else { require(PrintsMYSQL); } } else { $url = BASE_URL . 'login.php'; header("Location: $url"); }} Same code, just wrapped in a function. I then called the function in the same file instead of all the above code, as follows: set_con(); It failed to make a connection. I would've thought wrapping it in a function would make no difference. It's not a show stopper, just curious. Any ideas? Thanks Paul Link to comment Share on other sites More sharing options...
Recommended Posts