1

I'm getting this annoying error and haven't been able to fix it yet.

<b>Fatal error: Class 'Console' not found in /home/serellyn/public_html/HEIM/php/nieuwbeheer/console_overview.php on line 45.</b>

Let's first start with the hierarchy which is like this.

index (main page) console_overview (section of page) include/connect (connect to DB) include/console.class (the class)

The index.php requires the connect.php and the console.class.php and loads the console_overview.php. Here's the code:

<?php
require_once('include/connect.php');
require_once('include/console.class.php');
var_dump(file_exists('include/connect.php'));
var_dump(file_exists('include/console.class.php'));
?>

<div id="mainpage" class="main-container inner">
    <?php
        if (isset($_GET['page'])) {
            $page = $_GET['page'];
        } else {
            $page = "console_overview";
        }
    ?>
</div>
<!-- end: MAIN CONTAINER -->

<script>
var page = "<?php echo $page;?>";
$( "#mainpage" ).load( page + ".php" );
</script>

I've used var_dumps to check if both file exists (and they do). The console_overview.php loads correctly. Now in the console_overview.php I'm trying to get data from the Console class, as following:

<?php
foreach(Console::getAllConsoles() as $aConsole) {
    $consoleID= $aConsole->getID();
    $consoleName = $aConsole->getName();
    $consoleHostname = $aConsole->getHostname();
    $consoleGameID = $aConsole->getGameID();
    $consolePowerState = $aConsole->getPowerState();

    echo "<tr>";
    echo "1";
}
?>

The error I'm getting is caused by the foreach... but I can't find out what's wrong...

The Console class looks like this (I'm pasting the most important parts, otherwise the code would be too long).

<?php

class Console{

private $ID, $hostname, $mac, $ip, $roomID, $gameID, $register, $powerState, $dateUpdated;  

public function Console($tID, $tHostname, $tMac, $tIp, $tRoomID, $tGameID, $tRegister, $tPowerState, $tDateUpdated) {
    $this->ID = $tID;
    $this->hostname = $tHostname;
    $this->mac = $tMac;
    $this->ip = $tIp;
    $this->roomID = $tRoomID;
    $this->gameID = $tGameID;
    $this->register = $tRegister;
    $this->powerState = $tPowerState;
    $this->dateUpdated= $tDateUpdated;
}
...
public static function getAllConsoles() {
    $sql = "SELECT * FROM `console` ORDER BY `hostname` ASC";
    $result = mysql_query($sql);
    $theResults = array();
    while ($row = mysql_fetch_array($result)) {
        $theResults[] = new      Console($row[0],$row[1],$row[2],$row[3],$row[4],$row[5],$row[6],$row[7],$row[8],$row[9],$row[10]);
    }
    return $theResults;
}

}

?>

So can anyone see what the problem is? Thank you for your help.

Edit: O and yes, I know MySQL is deprecated and will change this whenever the issue of not finding the console is fixed =).

3
  • 1
    You can't include a file with javascript and expect it to understand what's going on in PHP. These are two seperate PHP calls. Every time you get a PHP site off a website (unless you use require inside PHP) it creates a new request towards the server. You need to include the PHP files in the javascript loaded PHP files. Commented Sep 1, 2014 at 8:04
  • Side note: The mysql_* functions you are using are becoming deprecated and will be removed from future versions of PHP. You should not use them to write new code. Use mysqli_* or PDO instead. Commented Sep 1, 2014 at 8:07
  • I get it now, and yes Gerald, thanks for the warning. I do know this and is on my to do list ;) Commented Sep 1, 2014 at 13:21

1 Answer 1

4

Your console_overview.php does not include the required files. When you make an AJAX call with JavaScript from the client it is a separate HTTP request to the server, so you have to add the require() call again there:

<?php
require_once('include/connect.php');
require_once('include/console.class.php');
foreach(Console::getAllConsoles() as $aConsole) {
    $consoleID= $aConsole->getID();
    $consoleName = $aConsole->getName();
    $consoleHostname = $aConsole->getHostname();
    $consoleGameID = $aConsole->getGameID();
    $consolePowerState = $aConsole->getPowerState();

    echo "<tr>";
    echo "1";
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, right, thank you! One more thing, I'm getting the following error now: mysql_fetch_array() expects parameter 1 to be resource, boolean given. It's from $result = mysql_query($sql); $theResults = array(); while ($row = mysql_fetch_array($result)) { $theResults[] = new Console($row[0],$row[1],$row[2],$row[3],$row[4],$row[5],$row[6],$row[7],$row[8],$row[9],$row[10]); } Any idea why?
echo mysql_error(); is your friend. This question has been answered over and over on SO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.