0

PHP Fatal error: Call to a member function num_rows() on a string in user_model.php line 46. where line 46 is "$users = $this->db->query($query)->result(); "

its a customer subscription package finder code I am trying to parse result where customer subscription is expired or not valid,

<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class User_Model extends Base_Model  
{

    function __construct()
    {
        parent::__construct();
    }


    /*** package expire***/
    function package_expire()
    {
        $query = "SELECT u.id,u.is_premium FROM ".$this->db->dbprefix(TBL_USERS)." u, ".$this->db->dbprefix(TBL_USERS_GROUPS)." ug WHERE u.id=ug.user_id AND ug.group_id='2'
        AND u.is_premium='1' ";

        $users = $this->db->query($query)->result();    

        if(count($users)>0) 
        {
            foreach($users as $u) :
                if($u->is_premium != 0) 
                {
                    $user_subscription = $this->base_model->fetch_records_from(TBL_SUBSCRIPTIONS,array('user_id'=>$u->id,'status'=>'Active'));

                    if(count($user_subscription)>0)
                    {
                        $user_subscription = $user_subscription[0];

                        if(isset($user_subscription->expire_date) && strtotime(date('Y-m-d')) > strtotime($user_subscription->expire_date))
                        {
                            $data['status']         = 'In-Active';
                            $whr['subscription_id'] = $user_subscription->subscription_id;

                            if($this->base_model->update_operation($data,TBL_SUBSCRIPTIONS,$whr))
                            {
                                $user_data['is_premium'] = '0';
                                $user_data['package_id'] = ' ';
                                $user_data['no_of_package_quotes'] = 0;
                                $user_data['no_of_package_quotes_used'] = 0;

                                $user_whr['id'] = $u->id;
                                $this->base_model->update_operation($user_data,TBL_USERS,$user_whr);
                            }                                   
                        }
                    }   
                }
            endforeach;
        }
    }

    function getUsers($conditions=array(),$limit=NULL)
    {
        $str='';
        if(count($conditions)>0)
        {
            if(isset($conditions['admin_read_status']))
                $str .= 'AND u.admin_read_status = "'.$conditions['admin_read_status'].'"';
        }

        $query  = 'SELECT u.*,ug.user_id,ug.group_id FROM '.TBL_PREFIX.TBL_USERS.' u 
                    INNER JOIN '.TBL_PREFIX.TBL_USERS_GROUPS.' ug ON u.id=ug.user_id 
                    AND ug.group_id='.GRP_USER.' '.$str.' ';
        $resultsetlimit = $this->db->query( $query );
        $this->numrows = $this->db->affected_rows();
        if($limit != '') $query = $query . ' LIMIT '.$limit;
        $resultsetlimit = $this->db->query( $query );
        return $resultsetlimit->result();           

    }

    function userSubscriptions($user_id=NULL,$conditions=array())
    {
        if($user_id=='')
            return false;

        $str='';
        if(count($conditions)>0)
        {
            if($conditions['status'])
                $str .= 'AND s.status = "'.$conditions['status'].'"';
            if($conditions['subscription_id'])
                $str .= 'AND s.subscription_id = "'.$conditions['subscription_id'].'"';
        }

        $query  = 'SELECT s.*,u.* FROM '.TBL_PREFIX.TBL_SUBSCRIPTIONS.' s 
                    INNER JOIN '.TBL_PREFIX.TBL_USERS.' u ON s.user_id=u.id 
                    AND u.id='.$user_id.' '.$str.' ';
        $resultsetlimit = $this->db->query( $query );
        $this->numrows = $this->db->affected_rows();
        $resultsetlimit = $this->db->query( $query );
        return $resultsetlimit->result();                   
    }
}
?>
Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
Suraj Sharma
  • 1
  • 1
  • 5
  • what are you actually asking? do you want to use result? why do you want to use result? what are you trying to achieve? – treyBake Apr 17 '18 at 16:15
  • this function is used for admin panel where admin can see which of the subscriber has subscription or subscription is expired, – Suraj Sharma Apr 17 '18 at 16:22
  • Of what type is `$this->db`? – Joas Apr 17 '18 at 16:27
  • what do you mean by what type if $this-> db? elaborate please – Suraj Sharma Apr 17 '18 at 16:29
  • What is stored in the `$this->db` var the line the code errors? What type of database connection is used? You specify 2 errors in your question which both have to do with the variabele, but don't give any info about it @SurajSharma – Joas Apr 17 '18 at 16:43
  • restored the old files where i got the first error – Suraj Sharma Apr 17 '18 at 16:55
  • 1
    Please don't edit your post to such extent where the answers to your question are no longer relevant. If you have a follow up question you can post a new one. – Alon Eitan Apr 18 '18 at 09:55
  • after a long research i found that i am getting this type of error on everywhere, in every packages or every project, i am using multi domain linux hosting , i tried to install any other php project and getting the fatal error Call to undefined function – Suraj Sharma Apr 18 '18 at 16:44

2 Answers2

0

Looks like you are trying to get number of affected rows from a database query. The problem is the class you are using is calling on a variable or function that doesn't exist.

A fix would be to declare it after opening your class. eg:

class User_Model extends Base_Model  
{
 public $num_rows = 0;

You will then be able to use that value after you have run queries.

 $user_model = new User_Model();
 $query = $user_model->userSubscriptions(whatever goes in here);
 echo 'There were ' .$user_model->num_rows.' affected by this query';
Second2None
  • 1,470
  • 1
  • 12
  • 22
  • it was working nicely, suddenly I got an error (PHP Fatal error:" Call to a member function result() on boolean " on another file called ion_auth.php, i updated that file somehow and getting this error. – Suraj Sharma Apr 17 '18 at 16:44
  • Can you post the class Base_Model ? – Second2None Apr 17 '18 at 16:55
  • Updating var $numrows; to public $numrows; this will give it the proper scope the variable needs. [link](https://stackoverflow.com/questions/4361553/what-is-the-difference-between-public-private-and-protected) From your updated post at the top the problem could be coming from class CI_Model, can you do a quick pastebin of that aswell – Second2None Apr 17 '18 at 17:42
  • // Debugging note: // If you're here because you're getting an error message // saying 'Undefined Property: system/core/Model.php', it's // most likely a typo in your model code. – Second2None Apr 18 '18 at 06:46
  • after a long research i found that i am getting this type of error on everywhere, in every packages or every project, i am using multi domain linux hosting , i tried to install any other php project and getting the fatal error Call to undefined function – Suraj Sharma Apr 18 '18 at 16:44
0
function __construct($db) {
    parent::__construct($db);
    $this->db = $db;
}

You've forgotten to pass the database object, as your code stands the User_Model object is looking for the member $this->db variable (object) which it can't find.

SpacePhoenix
  • 607
  • 1
  • 5
  • 15
  • eestored the old files where i got the first error , it was working properly earlier, suddenly getting this error. i tried to change the ion_auth,php but getting error – Suraj Sharma Apr 17 '18 at 16:57