Bman900 Posted May 13, 2009 Share Posted May 13, 2009 define("DB_SERVER", "db854.pedrfora.net"); define("DB_USER", "dbo28387dd5085"); define("DB_PASS", "XjT.WtddW"); define("DB_NAME", "db28387dd5085"); I want to set it up so it would be like: define("DB_SERVER", "$host"); define("DB_USER", "$username"); define("DB_PASS", "$password"); define("DB_NAME", "$name"); I have those variables defined in a config.php file. I already tried the above set up and it doesn't work for some reason. Am I doing something wrong? Quote Link to comment https://forums.phpfreaks.com/topic/158035-insert-variable-into-define-function/ Share on other sites More sharing options...
jackpf Posted May 13, 2009 Share Posted May 13, 2009 What's going wrong? And you don't need quotes round variables. Quote Link to comment https://forums.phpfreaks.com/topic/158035-insert-variable-into-define-function/#findComment-833632 Share on other sites More sharing options...
Bman900 Posted May 13, 2009 Author Share Posted May 13, 2009 Well here is my real code: include ("../config.php"); define("DB_SERVER", $dbhost); define("DB_USER", $dbusername); define("DB_PASS", $dbpassword); define("DB_NAME", $databasename); and then here is the error I get when I run it: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) Quote Link to comment https://forums.phpfreaks.com/topic/158035-insert-variable-into-define-function/#findComment-833636 Share on other sites More sharing options...
Potatis Posted May 13, 2009 Share Posted May 13, 2009 Isn't this your config.php? define("DB_SERVER", $dbhost); define("DB_USER", $dbusername); define("DB_PASS", $dbpassword); define("DB_NAME", $databasename); Are you trying to include config.php in your config.php file? ??? Personally I define like you did the first time. <?php define("DB_SERVER", "host"); define("DB_USER", "user"); define("DB_PASS", "pass"); define("DB_NAME", "dbname"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/158035-insert-variable-into-define-function/#findComment-833639 Share on other sites More sharing options...
jackpf Posted May 13, 2009 Share Posted May 13, 2009 This seems like a pretty odd way of doing things anyway. If you've got variables already set, use them. If you want to use constants, just use constants instead. It sounds stupid to me to have both set to the same thing. Quote Link to comment https://forums.phpfreaks.com/topic/158035-insert-variable-into-define-function/#findComment-833642 Share on other sites More sharing options...
Bman900 Posted May 13, 2009 Author Share Posted May 13, 2009 I have a config.php file that will define all my databse info. I then used a log in system that has constants.php I just simply want to use my variables defining my database info in my constants.php so people won't have to edit two file when they install the script. Quote Link to comment https://forums.phpfreaks.com/topic/158035-insert-variable-into-define-function/#findComment-833645 Share on other sites More sharing options...
jackpf Posted May 13, 2009 Share Posted May 13, 2009 Well if you just used variables or constants, then you'd only have to edit one file as well. But what's not working with your definitions? Quote Link to comment https://forums.phpfreaks.com/topic/158035-insert-variable-into-define-function/#findComment-833647 Share on other sites More sharing options...
Bman900 Posted May 13, 2009 Author Share Posted May 13, 2009 Well the values are passed on correctly as I echoed everything out. I then moved everything into the constant.php from the config.php so it was $dbhost = "db854.perfora.net"; $dbusername = "dbo283875085"; $dbpassword = "XjT.WtgW"; $databasename = "db283875085"; define("DB_SERVER", $dbhost); define("DB_USER", $dbusername); define("DB_PASS", $dbpassword); define("DB_NAME", $databasename); which worked. This is what that doesnt work: include ("../config.php"); define("DB_SERVER", $dbhost); define("DB_USER", $dbusername); define("DB_PASS", $dbpassword); define("DB_NAME", $databasename); Quote Link to comment https://forums.phpfreaks.com/topic/158035-insert-variable-into-define-function/#findComment-833648 Share on other sites More sharing options...
jackpf Posted May 13, 2009 Share Posted May 13, 2009 And is the file that defines the constants including the file that sets the variables? Quote Link to comment https://forums.phpfreaks.com/topic/158035-insert-variable-into-define-function/#findComment-833649 Share on other sites More sharing options...
Bman900 Posted May 13, 2009 Author Share Posted May 13, 2009 Yea that would be the config.php you see up there. Quote Link to comment https://forums.phpfreaks.com/topic/158035-insert-variable-into-define-function/#findComment-833658 Share on other sites More sharing options...
jackpf Posted May 13, 2009 Share Posted May 13, 2009 Hmm...try echoing out the variables in where you're defining them. Chances are you're script isn't inheriting them. Quote Link to comment https://forums.phpfreaks.com/topic/158035-insert-variable-into-define-function/#findComment-833659 Share on other sites More sharing options...
Bman900 Posted May 13, 2009 Author Share Posted May 13, 2009 It echoes out correctly but what I noticed is that if the config.php is in the same folder as constants.php it would work but when i tried it from my directory using ../config.php it didn't work but get this it still echoed out the variables correctly. I have never seen this before... Quote Link to comment https://forums.phpfreaks.com/topic/158035-insert-variable-into-define-function/#findComment-833666 Share on other sites More sharing options...
Bman900 Posted May 14, 2009 Author Share Posted May 14, 2009 Alright so I believe the problem was that include ("../config.php"); was looking for that file in my site directory while I had my file in another folder. This is what I had to do to make it work. include ("../draft/config.php"); So my config file was draft/config.php While my constants was in draft/include/constants.php ("../config.php"); worked for all my other files that needed the database variable but those were inside draft/admin folder which is basically the same thing as draft/include My question is what is the correct way of going back only one folder since ("../config.php"); takes it back to the directory....? Quote Link to comment https://forums.phpfreaks.com/topic/158035-insert-variable-into-define-function/#findComment-833685 Share on other sites More sharing options...
jackpf Posted May 14, 2009 Share Posted May 14, 2009 ../ is one directory up. You should use require() if you're not sure your scripts are being included. Quote Link to comment https://forums.phpfreaks.com/topic/158035-insert-variable-into-define-function/#findComment-833943 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.