致命错误:在非对象上调用成员函数num_rows()

时间:2011-05-01 06:18:33

标签: php fatal-error

你好吗?

我试图用特定的employee_id来计算基本工资,但当我试图......给我致命的错误..

致命错误:在第112行的D:\ wamp \ www \ template \ application \ models \ salary.php中的非对象上调用成员函数num_rows()

我的代码是:model

<?php
class Salary extends Model
{   
    /*
    Determines if a given person_id is a profile
    */
    function exists($salary_id)
    {
        $this->db->from('salary_scale');
        $this->db->where('id',$salary_id);
        $this->db->where('deleted',0);      
        $query = $this->db->get();

        return ($query->num_rows()==1);
    }
    /*
    Determines if a given employee_id is a employee
    */
    function exists_employee($employee_id)
    {
        $this->db->from('grade_history');
        $this->db->where('employee_id',$employee_id);
        $query = $this->db->get();

        return ($query->num_rows()==1);
    }

    /*
    Returns all the suppliers
    */
    function get_all()
    {
        $this->db->from('salary_scale');
        $this->db->where('deleted', 0);
        $this->db->order_by("name", "asc");
        return $this->db->get();
    }

        /*
         *
         *  Gets information about a particular employees salary
     */
    function grade_rules_info($salary_grade)
    {
        $this->db->from('salary_scale_rules');
        $this->db->where('salary_grade',$salary_grade);
        $query = $this->db->get();

        if($query->num_rows()==1)
        {
            return $query->row();
        }
        else
        {
            //Get empty base parent object, as $item_id is NOT an item
            $salary_obj=new stdClass();

            //Get all the fields from items table
            $fields = $this->db->list_fields('salary_scale_rules');

            foreach ($fields as $field)
            {
                $salary_obj->$field='';
            }

            return $salary_obj;
        }
    }

        /*
         *
         *  Gets information about a particular employees salary
     */
    function get_info($employee_id)
    {
        $this->db->from('allowance');
        //$this->db->join('deductions', 'deductions.eid = allowance.eid');
        $this->db->where('employee_id',$employee_id);
        $query = $this->db->get();

        if($query->num_rows()==1)
        {
            return $query->row();
        }
        else
        {
            //Get empty base parent object, as $item_id is NOT an item
            $salary_obj=new stdClass();

            //Get all the fields from items table
            $fields = $this->db->list_fields('items');

            foreach ($fields as $field)
            {
                $salary_obj->$field='';
            }

            return $salary_obj;
        }
    }

        /**
         * Gets information about a particular employees salary
         *
     **/
    function get_grade_info($employee_id)
    {
        $this->db->from('grade_history');
        $this->db->where('employee_id',$employee_id);
        $query = $this->db->get();

        if($query->num_rows()==1)
        {
            return $query->row();
        }
        else
        {
            //Get empty base parent object, as $employee_id is NOT an employee
            $grade_obj=new stdClass();

            //Get all the fields from items table
            $fields = $this->db->list_fields('grade_history');

            foreach ($fields as $field)
            {
                $grade_obj->$field='';
            }

            return $grade_obj;
        }
    }

        /**
     * Inserts or updates configuration data
         *
     */

    function save_grade(&$data, $employee_id=false)
    {
        if (!$employee_id)
        {
            if($this->db->insert('grade_history', $data))
            {
                $data['id']=$this->db->insert_id();
                return true;
            }
            return false;
        }

        $this->db->where('id', $employee_id);
        return $this->db->update('grade_history', $data);
    }

        // ------------------------ End of save_salary_cinfig function -----------------------------


        /*
         * Basic pay salary calculation
         */
        function basic_pay($employee_id)
        {
            // Get grade information from grade_history table
            // Get Particular person grade ifnormation by employ_id
            $grade_info = $this->get_grade_info($employee_id);

            $salary_grade           = $grade_info->salary_grade;
            $no_of_increment        = $grade_info->number_of_increment;

            // Get Grade rules information about particular grade by passing $salary_grade number

            $grade_rules            = $this->grade_rules_info($salary_grade);

            $basic_amount           = $grade_rules->basic_amount;
            $increment              = $grade_rules->increment;
            $max_no_of_increment    = $grade_rules->number_of_increment;
            $max_amount             = $grade_rules->max_amount;

            return $basic_pay = $basic_amount + $increment*$no_of_increment;


        }
}
?>

controller

<?php
require_once ("secure_area.php");
class Salaries extends Secure_area
{
    function __construct()
    {
        parent::__construct('salaries');
    }

    function index()
    {
        $data['controller_name']=strtolower($this->uri->segment(1));
        $data['form_width']=$this->get_form_width();
        $data['manage_table']=get_profile_manage_table($this->Profile->get_all(),$this);
        $this->load->view('salaries/manage',$data);
    }

    /*
    Returns profile table data rows. This will be called with AJAX.
    */
    function search()
    {
        $search=$this->input->post('search');
        $data_rows=get_profile_manage_table_data_rows($this->Profile->search($search),$this);
        echo $data_rows;
    }

    /*
    Gives search suggestions based on what is being searched for
    */
    function suggest()
    {
        $suggestions = $this->Profile->get_search_suggestions($this->input->post('q'),$this->input->post('limit'));
        echo implode("\n",$suggestions);
    }

    /*
    Loads the profile form
    */
    function view($employee_id=-1)
    {
        //$data['salary_summary_info']  =$this->Salary->get_info($salary_id);
        //$data['salary_deductions_summary_info'] =$this->Salary->get_deductions_info($salary_id);

                $employee_id = array('' => '-- Select Employee ID --');
                foreach($this->Profile->get_employee_id()->result_array() as $row)
        {
            $employee_id[$row['employee_id']]= $row['employee_id'];
        }
                $data['employee_id'] = $employee_id;
               // $data['selected_employee_id'] = $this->Profile->get_info($employee_id)->employee_id;

                $data['basic_pay'] = $this->Salary->basic_pay($employee_id);

        $this->load->view("salaries/salary_summary_form", $data);
    }
        //--------------------------------------End view function-----------------------------------

    /*
    Loads the profile form
    */
    function grade_view($employee_id=-1)
    {
        $data['salary_grade_info']  = $this->Salary->get_grade_info($employee_id);

                $data['basic_pay'] = $this->Salary->basic_pay($employee_id);

        $this->load->view("salaries/grade_view", $data);
    }
        //--------------------------------------End view function-----------------------------------


    /*
    Inserts/updates a profile
    */
    function save_salary_grade($id=-1)
    {
        $grade_data = array(

                    'eid'                   =>$this->input->post('employee_id'),
                    'salary_grade'          =>$this->input->post('salary_grade'),
                    'number_of_increment'   =>$this->input->post('number_of_increment'),
                    'comments'              =>$this->input->post('comments'),
                    'date'                  =>date('Y-m-d H:i:s')

        );

        if($this->Salary->save_grade($grade_data, $id))

        {   //New profile
            if($id==-1)
            {
                echo json_encode(array('success'=>true,'message'=>$this->lang->line('profiles_successful_adding').' '.
                $grade_data['salary_grade'].' '.$grade_data['number_of_increment'],'id'=>$grade_data['eid']));
            }
            else //previous profile
            {
                echo json_encode(array('success'=>true,'message'=>$this->lang->line('profiles_successful_updating').' '.
                $grade_data['salary_grade'].' '.$grade_data['number_of_increment'],'id'=>$grade_data['eid']));
            }
        }
        else//failure
        {   
            echo json_encode(array('success'=>false,'message'=>$this->lang->line('profiles_error_adding_updating').' '.
            $grade_data['salary_grade'].' '.$grade_data['number_of_increment'],'id'=>$grade_data['id']));
        }
    }

    /*
    This deletes profiles from the profiles table
    */
    function delete()
    {
        $profiles_to_delete=$this->input->post('ids');

        if($this->Profile->delete_list($profiles_to_delete))
        {
            echo json_encode(array('success'=>true,'message'=>$this->lang->line('profiles_successful_deleted').' '.
            count($profiles_to_delete).' '.$this->lang->line('profiles_one_or_multiple')));
        }
        else
        {
            echo json_encode(array('success'=>false,'message'=>$this->lang->line('profiles_cannot_be_deleted')));
        }
    }

    /*
    get the width for the add/edit form
    */
    function get_form_width()
    {           
        return 900;
    }
}
?>

并查看

<?php
echo form_open('salaries/save/'.$employee_id, array('id'=>'salary_summary_form'));

//echo form_open('salaries/save/', array('id'=>'salary_summary_form'));
?>
<div id="required_fields_message"><?php echo $this->lang->line('common_fields_required_message'); ?></div>
<ul id="error_message_box"></ul>
<fieldset id="salary_allowance_info">
<legend><?php echo $this->lang->line("salaries_allowance_info"); ?></legend>

    <div class="field_row clearfix">
    <?php echo form_label($this->lang->line('salaries_employee_id').':', 'employee_id', array('class'=>'required')); ?>
            <div class='form_field'>
                <?php  echo form_dropdown('employee_id', $employee_id);?>
            </div>
    </div>

    <div class="field_row clearfix">
    <?php echo form_label($this->lang->line('salaries_allowance_basic_salary').':', 'basic_salary', array('class'=>'required')); ?>
            <div class='form_field'>
            <?php echo form_input(array(
                    'name'=>'basic_salary',
                    'id'=>'basic_salary',
                    'value'=>$basic_pay
                    ));
            ?>
            </div>
    </div>

    <div class="field_row clearfix">
    <?php echo form_label($this->lang->line('salaries_allowance_house_rent').':', 'house_rent',array('class'=>'required')); ?>
            <div class='form_field'>
            <?php echo form_input(array(
                    'name'=>'house_rent',
                    'id'=>'house_rent',
//                    'value'=>$salary_summary_info->house_rent
                    ));
            ?>
            </div>
    </div>



<?php
echo form_submit(array(
    'name'=>'submit',
    'id'=>'submit',
    'value'=>$this->lang->line('common_submit'),
    'class'=>'submit_button float_right')
);

?>
</fieldset>
<?php
echo form_close();
?>

请帮助我,如果有的话

2 个答案:

答案 0 :(得分:0)

你不测试你对$ db的调用 - &gt; get()成功了。我不知道你的$ db类的细节,但我怀疑只有在调用它成功的情况下它才会返回。如果查询失败,则为$ db - &gt; get()仍然返回一些东西?

尝试对你从$ db中获取的内容做一个var_dump - &gt; get()所以你可以看到它是否正在返回你认为它正在返回的东西。

答案 1 :(得分:0)

Fatal error: Call to a member function num_rows() on a non-object...通常是因为没有从给定查询返回结果或者可能来自错误查询。