0

here is my problem:

index.php contents:

require_once('phpcommonscripts/connections/connection.php');
require_once('phpcommonscripts/functions/logging.php');

    Func_LogToDB($logType, $actionType, $errMsg, $varUser);

connection.php contents:

$hostname_MYDB = "localhost";
$database_MYDB = "MYDB";
$username_MYDB = "USER";
$password_MYDB = "PASS";
$MYDB = mysql_pconnect($hostname_MYDB, $username_MYDB, $password_MYDB) or trigger_error(mysql_error(), E_USER_ERROR);

logging.php contents:

function Func_LogToDB($lType, $lAction, $lMessage, $lUser) {       
    mysql_select_db($database_MYDB, $MYDB);
}

ERROR MESSAGE:

Notice: Undefined variable: database_MYDB in /home/notes/public_html/phpcommonscripts/functions/logging.php on line 22

( line 22 is the mysql_select_db() line... )

Am I doing something wrong here ?

MirrorMirror
  • 186
  • 8
  • 36
  • 70
  • You should read up on [variable scope with PHP](http://php.net/manual/en/language.variables.scope.php) – adlawson Nov 06 '11 at 19:26

1 Answers1

1

Am I doing something wrong here ?

Yes you are :)

Those variables $database_MYDB, $MYDB aren't in same scope.

You cannot access variables which are defined outside a function from within a function.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • @user1032650: You can make them `global` but that is NOT advised. – PeeHaa Nov 06 '11 at 19:27
  • i added these two lines at logging.php: global $database_NOTES; global $NOTES; and works fine now. why would it not be advised what i did? – MirrorMirror Nov 06 '11 at 19:46
  • @user1032650: For more info about why `global` is considered bad practice see: http://stackoverflow.com/questions/1557787/are-global-variables-in-php-considered-bad-practice-if-so-why – PeeHaa Nov 06 '11 at 19:55