davidp Posted December 22, 2007 Share Posted December 22, 2007 I am having a problem accessing my dbm from my PHP script. Basically, the user comes to the page, and logs in. After logging in, the user can browse certain things on the page. Here is the code for index.php, which is quite short so don't worry: require_once ( "../../site_support/classes/admin.php" ); require_once ( "../../site_support/globals.php" ); require_once ( "secure.php" ); require_once ( "adminfunctions.php" ); require_once ( "../../site_support/classes/dbm.php" ); //Connect to the database global $dbm; $dbm = new DBM(); // Check if the database is available. if (!$dbm->isConnected()) { print 'Unable to access the database at this time.<p>Sorry for the inconvenience.'; exit; } if ( !isset($_SESSION['admin_account']) ) $_SESSION['admin_account'] = new Admin(); print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <head> <title>Exploring Borders Admin</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link rel="stylesheet" href="'.BASE_URL.'style/layout_admin.css" type="text/css"> </head> <body> <b><div style="font-size: 16pt;">Admin Access Page</div></b><br>'; if ( !$_SESSION['admin_account']->isLoggedIn() ) { $_SESSION['admin_account']->displayLoginForm(); } else displayAdminPage(); print '</body></html>'; ?> Now, in this part of the code I am able to connect to the SQLite database completely fine. This is evident in the fact that the code doesn't throw up the error message that I inserted in there, and logging in/logging out works perfectly. The problem is when I call the function displayAdminPage which is contained at the bottom of the code snippet that I just posted. displayAdminPage is located in "adminfunctions.php". Here is the body of that function: //This is the main function of the "adminfunctions". It calls the other display functions. function displayAdminPage ( ) { print '<div style="width: 800px;">'; displayAdminHeader(); displayAdminSideBar(); displayMainContent(); print '</div>'; } Inside my displayMainContent() function, I do this: if ( isset ($dbm) ) print "dbm is set."; else print "dbm is not set."; Now here is where the problem lies, the DBM is not set anymore. Once I start calling functions from adminfunctions.php, the DBM becomes undefined....but that I don't understand. I am making the function call from a place where the DBM is defined, so shouldn't it still be defined inside of the function itself? Anyone have a suggestion on how I can fix the problem? Quote Link to comment https://forums.phpfreaks.com/topic/82790-database-undefined/ Share on other sites More sharing options...
phpSensei Posted December 22, 2007 Share Posted December 22, 2007 try require_once ( "../../site_support/classes/dbm.php" ); require_once ( "../../site_support/classes/admin.php" ); require_once ( "../../site_support/globals.php" ); require_once ( "secure.php" ); require_once ( "adminfunctions.php" ); //Connect to the database global $dbm; $dbm = new DBM(); // Check if the database is available. if (!$dbm->isConnected()) { print 'Unable to access the database at this time.<p>Sorry for the inconvenience.'; exit; } if ( !isset($_SESSION['admin_account']) ) $_SESSION['admin_account'] = new Admin(); print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <head> <title>Exploring Borders Admin</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link rel="stylesheet" href="'.BASE_URL.'style/layout_admin.css" type="text/css"> </head> <body> <b><div style="font-size: 16pt;">Admin Access Page</div></b><br>'; if ( !$_SESSION['admin_account']->isLoggedIn() ) { $_SESSION['admin_account']->displayLoginForm(); } else displayAdminPage(); print '</body></html>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/82790-database-undefined/#findComment-421045 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.