Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 527947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:56:11+00:00 2026-05-13T08:56:11+00:00

I have a html page that calls a php object to get some data

  • 0

I have a html page that calls a php object to get some data back from the database. It works fine, but the script was getting unwieldy so I decided to break some of it out into a bunch of functions.

So I have the following files:

// htdocs/map.php
<?php
    include("config.php");
    include("rmap.php");

    $id = 1;
    $gamenumber = getGameNumber($id);
    echo $gamenumber;
?>

// htdocs/config.php
<?php
$_PATHS["base"]      = dirname(dirname(__FILE__)) . "\\";
$_PATHS["includes"]  = $_PATHS["base"] . "includes\\";

ini_set("include_path", "$_PATHS[includes]");
ini_set("display_errors", "1");
error_reporting(E_ALL); 

include("prepend.php");
?> 

// includes/prepend.php
<?php
include("session.php");
?>

// includes/session.php
<?php
includes("database.php");

class Session {
    function Session() {
    }

};

$session = new Session;
?>

// includes/database.php
<?php
include("constants.php");

class Database {

    var $connection;

    function Database() {
        $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
        mysql_select_db(DB_NAME, $this->connection) or die(mysql_error());
    }

    function query($query) {
        return mysql_query($query, $this->connection);
    }
};

/* Create database connection */
$database = new Database;
?>

// includes/rmap.php
<?php
function getGameNumber($id) {
    $query = "SELECT gamenumber FROM account_data WHERE usernum=$id";
    $result = $database->query($query); // line 5
    return mysql_result($result, 0);
}
?>

And constants has a bunch of defines in it (DB_USER, etc).

So, map.php includes config.php. That in turn includes prepend.php which includes session.php and that includes database.php. map.php also includes rmap.php which tries to use the database object.

The problem is I get a

Fatal error: Call to a member function on a non-object in
C:\Develop\map\includes\rmap.php on line 5

The usual cause of this is that the $database object isn’t created/initialised, but if I add code to show which gets called when (via echo "DB connection created"; and echo "using DB connection";) they are called (or at least displayed) in the correct order.

If I add include("database.php") to rmap.php I get errors about redefining the stuff in constants.php. If I use require_once I get the same errors as before.

What am I doing wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:56:11+00:00Added an answer on May 13, 2026 at 8:56 am

    $database is not in the scope of function GetGameNumber.

    The easiest, though not necessarily best, solution is

    <?php
    function getGameNumber($id) {
        global $database;  // added this
        $query = "SELECT gamenumber FROM account_data WHERE usernum=$id";
        $result = $database->query($query); // line 5
        return mysql_result($result, 0);
    }
    ?>
    

    global is deemed not perfect because it violates the principles of OOP, and is said to have some impact on performance because a global variable and all its members are added to the function’s scope. I don’t know how much that really affects performance, though.

    If you want to get into the issue, I asked a question on how to organize such things a while back and got some good answers and links.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML page that contains some filenames that i want to download
I have an HTML page that uses AJAX to retrieve messages from a server.
I have Jquery+Ajax page that connects to server gets some data, and displays it.
I have a php page where I add and delete items from database using
I have a script that calls a php page via jquery/ajax. The results would
I have the following handler in my page that works fine in Firefox, but
I have a html page that allows users to submit a file. Below is
I have an html page that open a popup window when the page loads.
I have an HTML page that roughly looks like this: <div id=rolesRollerBody style=height: 309px;>
Ok, so as the title says, I have an HTML page that I fetch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.