Page 1 of 1

A small problem with globals.

Posted: Tue May 13, 2003 7:01 am
by Gleeb
No, this is not a register_globals thing, I adapted to that quickly and now typr $_GET['whatever'] by instinct ;)

My problem is thus...

Code: Select all

<?php
   global $cfg;

   print_r($cfg);

   $dblink = @mysql_connect($cfg['dbhost'], $cfg['dbuser'], $cfg['dbpass'])
      or die ("Could not connect to mySQL database. $cfg['dbuser']@$cfg['dbhost'] - ".mysql_error());

?>
Outputs
Array
(
[dbuser] => intranet
[dbpass] => notreallythisbutitdlookstupidtohavethisblank
[dbhost] => localhost
)
<br />
<b>Notice</b>: Undefined index: 'dbuser' in <b>c:\intranet\dev\common.php</b> on line <b>25</b><br />
<br />
<b>Notice</b>: Undefined index: 'dbhost' in <b>c:\intranet\dev\common.php</b> on line <b>25</b><br />
Could not connect to mySQL database. @ - Access denied for user: 'intranet@127.0.0.1' (Using password: YES)
The mySQL error is correct, because I'm supplying the wrong password on purpose, and it works as expected when I put in the right password.

When I comment out the 'or die', it works perfectly. Is there a bug I should know about?

Posted: Tue May 13, 2003 7:49 am
by volka
"Could not connect to mySQL database. $cfg['dbuser']@$cfg['dbhost'] - "
within a literal there's no need to mark another literal. In this case php tries to find a key 'dbuser' instead of dbuser. Try

Code: Select all

"Could not connect to mySQL database. $cfg[dbuser]@$cfg[dbhost] - '

Posted: Tue May 13, 2003 7:55 am
by Gleeb
I knew that ;)

Thanks for your help :)