Jump to content

Insert variable into define function


Bman900

Recommended Posts

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?

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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");

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

 

 

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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....?

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.