Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/82790-database-undefined/
Share on other sites

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

?>

Link to comment
https://forums.phpfreaks.com/topic/82790-database-undefined/#findComment-421045
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.