0

Hi i am trying to import the configuration variables from a config file that i had created

Config.php has

class Config
{
    public $SERVERNAME = '*******';
    public $SUSERNAME = '*********';
    public $SPASSWORD = '*********';
    public $DBNAME2 = '********';
}

for importing this variable in another file i tried

include('./Config.php') or die("error");
$cnfig = new Config();

But when i try to use $cnfig->DBNAME2 its not working

Please help to sole this problem.

Thank you.

EDIT

It was working in localsystem , when i tied to upload through web hosting it was not working.

krishna kumar
  • 153
  • 1
  • 6
  • 14

1 Answers1

1

Try below one

include  ('./Config.php');
$cnfig = new Config();
echo "<pre>";print_r($cnfig);

Edit:

I am getting below output so it should be working for you too.

Config Object
(
    [SERVERNAME] => *******
    [SUSERNAME] => *********
    [SPASSWORD] => *********
    [DBNAME2] => ********
)

Edit:2

You can also do like this

(include('./Config.php')) or die("error");
$cnfig = new Config();
echo "<pre>";print_r($cnfig);

http://www.php.net/manual/en/function.include.php#39043

or has higher priority than include()

Error Ouput:

When only use include('./Config.php') or die("error"); it's giving below error.


Warning: include(1) [function.include]: failed to open stream: No such file or directory in D:\path_to_file\myConfig.php on line 2

Warning: include() [function.include]: Failed opening '1' for inclusion (include_path='.;D:\ZendServer\share\ZendFramework\library') in D:\path_to_file\myConfig.php on line 2

Fatal error: Class 'Config' not found in D:\path_to_file\myConfig.php on line 6

Smile
  • 2,770
  • 4
  • 35
  • 57
  • Tried,this was also not printing anything. – krishna kumar May 14 '13 at 13:11
  • That was the problem,what is working for every one was not working for me. – krishna kumar May 14 '13 at 13:17
  • even Edit:2 gives same result. – krishna kumar May 14 '13 at 13:22
  • Above should work because your code `include('./Config.php') or die("error");` is giving error. – Smile May 14 '13 at 13:23
  • when i remove $cnfig = new Config(); all lines below it started working.So problem is there for only declaring the object. – krishna kumar May 14 '13 at 13:27
  • With your original code, Are you not getting any error message when you try to include your Config.php file?` – Smile May 14 '13 at 13:30
  • You should get error message. Try to put `error_reporting(E_ALL);` at the top of your page. – Smile May 14 '13 at 13:33
  • Then there should be some other things going on. Because above solution should work as first I was getting error with your original code but then I corrected code and posted as answer and same must be worked for you. – Smile May 14 '13 at 13:40
  • was it good to directly specify DB username/password in connect statement without importing any variables? – krishna kumar May 14 '13 at 13:40
  • You should use `PDO` like `$db = new PDO("mysql:host=YourHost;dbname=YourDBName", "YourUserName", "YourPassword");` but we your're using mysql_*() then `mysql_connect("YourHost", "YourUserName", "YourPassword");` But mysql_*() are strongly discouraged and they are deprecated. – Smile May 14 '13 at 13:43
  • ok,if the files were in same folder then the path which i gave was correct or need to be changed? – krishna kumar May 14 '13 at 13:47
  • File inclusion path is Correct – Smile May 14 '13 at 13:48
  • ok,then i will go with directly specifying the parameters instead of getting from a global variable.And Thank you all for taking time to solve my problem. – krishna kumar May 14 '13 at 13:51
  • just now i found even if i specify wrong path or file name in include it does not shows error.so there are some basically some problems.I need to find whats causing that. – krishna kumar May 14 '13 at 13:54
  • To know more about How to display error? Visit this SO(http://stackoverflow.com/questions/6575482/php-how-do-i-enable-error-reporting) link – Smile May 14 '13 at 14:54