phpmysqlajaxphpbb3

SQL Class Issue - Call to a member function on null - phpBB


I know this exact problem has been posted, but none give me a working answer. I have a simple PHP file that includes another php file to instantiate a few classes so that I can connect to my database. I get the error "Call a to member function sql_query() on null". The line that is getting the error is $result = $db->sql_query($sql);. I know that the include($phpbb_root_path . 'common.' . $phpEx); works. I have done var_dump($db) and it is not null, and I do not set that variable anywhere in this file, but in common.php which is included.Here is my php file:

PHP

<?php
//error reporting
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(E_ALL);

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

//handle ajax post data
if(isset($_POST['action']) && !empty($_POST['action'])) {
		$action = $_POST['action'];
		
		switch($action) {
			case 'get_vehicle_makes' :
				get_vehicle_makes();
				break;
			case 'get_vehicle_models' :
				get_vehicle_models();
				break;
			case 'test' :
				get_vehicle_years();
				break;
		}
	}

//The vehicle years list will always be the same regardless of make or model  - block_var = "vehicle_years"
	function get_vehicle_years()
	{
		$sql = 'SELECT DISTINCT year FROM phpbb_vehicles';	
		var_dump($sql);
		$result = $db->sql_query($sql);
		
		$data = array();
		
		while($row = $db->sql_fetchrow($result))
		{
			$data = $row['year'];
		}
		
		$db->sql_freeresult($result);
		
		$myArray = array(1, 2, 3);
		echo ("good job");
		exit;
	}


?>

As you can see at the top of my PHP, I include debugging code to display my php log errors, but I get nothing other than the error message. This isn't something obvious, is it?


Solution

  • You should pass $db to the function(s) as a parameter, else it wont be in the functions scope, though your global functions would suit a vehicle class, which then you only need to pass the $db instance once.

    For example:

    <?php
    //error reporting
    ini_set('display_startup_errors', 1);
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    
    define('IN_PHPBB', true);
    $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
    $phpEx = substr(strrchr(__FILE__, '.'), 1);
    include($phpbb_root_path . 'common.' . $phpEx);
    
    class vehicle {
    
        public function __construct($db)
        {
            $this->db = $db;
        }
    
        //The vehicle years list will always be the same regardless of make or model  - block_var = "vehicle_years"
        public function years()
        {
            $sql = 'SELECT DISTINCT year FROM phpbb_vehicles';  
            var_dump($sql);
            $result = $this->db->sql_query($sql);
    
            $data = array();
    
            while ($row = $this->db->sql_fetchrow($result)) {
                $data = $row['year'];
            }
    
            $this->db->sql_freeresult($result);
    
            $myArray = array(1, 2, 3);
            echo ("good job");
            exit;
        }
    
        //.. your other functions
    }
    
    //handle ajax post data
    if (isset($_POST['action']) && !empty($_POST['action'])) {
        $action = $_POST['action'];
    
        $vehicle = new vehicle($db);
    
        switch($action) {
            case 'get_vehicle_makes' :
                $vehicle->makes();
                break;
            case 'get_vehicle_models' :
                $vehicle->models();
                break;
            case 'test' :
                $vehicle->years();
                break;
        }
    }