Jump to content

fetch_array() on a non-object


Go to solution Solved by Barand,

Recommended Posts

Hi,

 

I'm getting the following error and im not entirely sure why - Call to a member function fetch_array() on a non-object

 

When I execute the SQL query manually in phpmyadmin it displays 'total' a value of as 7000 which is correct - so the query appears correct.

 

Could anyone point me in the right direction?

 

Something simple im missing.

    public function get_staff_current_month($staff_id)
    {
    	$db = new sql();
    	$sql = "SELECT SUM(amount) as total FROM $this->tablename WHERE staff_id = $staff_id AND YEAR(date_added) = YEAR(CURDATE()) AND MONTH(date_added) = MONTH(CURDATE()) ORDER BY id ASC";
    	$result = $db->query($sql);
    	//var_dump($result);
    	$row = $result->fetch_array();
    	return $row['total'];
    }

Regards,

 

MoFish.

Link to comment
https://forums.phpfreaks.com/topic/285305-fetch_array-on-a-non-object/
Share on other sites

without knowing the methods inside your sql class

 

we would only be guessing

<?php
class sql
{
	
	var $name;
	var $host;
	var $username;
	var $password;
	var $mysql_link;	
	var $sql_query;	
	var $sql_result;
	var $error;
	
	function sql()
	{
		$this->name=MYSQL_DB;
		$this->host=MYSQL_HOST;
		$this->username=MYSQL_USER;
		$this->password=MYSQL_PASS;
		$this->connection();
	}
	
	function connection()
	{
		$this->mysql_link=mysql_connect($this->host,$this->username,$this->password) or die ('Error connecting to MySQL');
		mysql_select_db($this->name, $this->mysql_link) or die('Database '.$db['db'].' does not exist!');
	}
	
	function query($sql_query)
	{
		
		$this->sql_query=$sql_query;
		$this->sql_result=mysql_query($this->sql_query);
		$this->error=mysql_error();
		if (!$this->sql_result) 
		{
			echo "<b>" . $sql_query . "</b><br><br>";
        	$this->error=mysql_error();
        	echo $this->error;
        }
		echo mysql_error();
	}
	
	function num_rows() 
    { 
        $mysql_rows = mysql_num_rows( $this->sql_result ); 
        return $mysql_rows; 
    } 
    
    function fetch_array() 
    { 
        if ( $this->num_rows() > 0 ) 
        { 
            $mysql_array = mysql_fetch_array( $this->sql_result ); 
            if (!is_array( $mysql_array )) 
            { 
                return FALSE; 
            } 
            return $mysql_array; 
        } 
    }
    
    function insert_id()
    {
    	return mysql_insert_id();
    }

}

?>

you also should not be creating a new instance of your database class inside of another class method just to run each query.

 

Should I therfore be setting db one at the first instance?

 

Thank you for your help.

  • Solution

try

public function get_staff_current_month($staff_id)
{
    $db = new sql();
    $sql = "SELECT SUM(amount) as total FROM $this->tablename WHERE staff_id = $staff_id AND YEAR(date_added) = YEAR(CURDATE()) AND MONTH(date_added) = MONTH(CURDATE()) ORDER BY id ASC";
    $db->query($sql);
    $row = $db->fetch_array();
    return $row['total'];
}

Create the connection to the database once. Pass it as a parameter to the functions that need it

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.