CodeIgniter奇怪的行为

时间:2013-01-30 14:35:26

标签: php json codeigniter session activerecord

由于这个原因,我正在撞到墙上

我知道这是一个很长的问题,但这就是我所拥有的:(

我在CI工作超过一年,但这个错误让我感到害怕 问题是我从模型中获取数据,因此返回

Unknown column 'albums' in 'where clause'</p><p>SELECT *
FROM (`albums`)
WHERE `id` =  '1'
AND `albums` IS NULL
AND `id` =  '1'

模型功能

function get($id = 0 )
   {
        if($id == 0 )
        {
            $q =  $this->db->get('albums')->result(); 
            return $q; 
        }
        else
        {
            $this->db->where("id",$id); 
            $q = $this->db->get('albums'); 
            return $q->row(); 
        }
   }

如果我删除了对该函数的调用,我会从json_encode中获得另一个错误 当我调试$ data时,我得到以下内容

Array
(
    [response] => CI_DB_mysql_driver Object
        (
            [dbdriver] => mysql
            [_escape_char] => `
            [_like_escape_str] => 
            [_like_escape_chr] => 
            [delete_hack] => 1
            [_count_string] => SELECT COUNT(*) AS 
            [_random_keyword] =>  RAND()
            [use_set_names] => 
            [ar_select] => Array
                (
                )

            [ar_distinct] => 
            [ar_from] => Array
                (
                )

            [ar_join] => Array
                (
                )

            [ar_where] => Array
                (
                    [0] => `id` =  '1'
                    [1] => AND `albums` IS NULL
                )

            [ar_like] => Array
                (
                )

            [ar_groupby] => Array
                (
                )

            [ar_having] => Array
                (
                )

            [ar_keys] => Array
                (
                )

            [ar_limit] => 
            [ar_offset] => 
            [ar_order] => 
            [ar_orderby] => Array
                (
                )

            [ar_set] => Array
                (
                    [`title`] => ' asdasd'
                    [`description`] => 'Hello '
                )

            [ar_wherein] => Array
                (
                )

            [ar_aliased_tables] => Array
                (
                )

            [ar_store_array] => Array
                (
                )

            [ar_caching] => 
            [ar_cache_exists] => Array
                (
                )

            [ar_cache_select] => Array
                (
                )

            [ar_cache_from] => Array
                (
                )

            [ar_cache_join] => Array
                (
                )

            [ar_cache_where] => Array
                (
                )

            [ar_cache_like] => Array
                (
                )

            [ar_cache_groupby] => Array
                (
                )

            [ar_cache_having] => Array
                (
                )

            [ar_cache_orderby] => Array
                (
                )

            [ar_cache_set] => Array
                (
                )

            [ar_no_escape] => Array
                (
                )

            [ar_cache_no_escape] => Array
                (
                )

            [username] => root
            [password] => 123456
            [hostname] => localhost
            [database] => sawt
            [dbprefix] => 
            [char_set] => utf8
            [dbcollat] => utf8_general_ci
            [autoinit] => 1
            [swap_pre] => 
            [port] => 
            [pconnect] => 1
            [conn_id] => Resource id #30
            [result_id] => 1
            [db_debug] => 1
            [benchmark] => 0.00059294700622559
            [query_count] => 1
            [bind_marker] => ?
            [save_queries] => 1
            [queries] => Array
                (
                    [0] => UPDATE `pulling_questions` SET `status` = 0 WHERE `status` =  1 AND `end_date` <=  1359556250
                )

            [query_times] => Array
                (
                    [0] => 0.00059294700622559
                )

            [data_cache] => Array
                (
                )

            [trans_enabled] => 1
            [trans_strict] => 1
            [_trans_depth] => 0
            [_trans_status] => 1
            [cache_on] => 
            [cachedir] => 
            [cache_autodel] => 
            [CACHE] => 
            [_protect_identifiers] => 1
            [_reserved_identifiers] => Array
                (
                    [0] => *
                )

            [stmt_id] => 
            [curs_id] => 
            [limit_used] => 
            [stricton] => 
        )

)

上面的代码在另一个控制器中完美运行,如果我从视图中调用get,那么对模型的调用没有任何错误地完成

我在此之前调用更新,但不会更新

function update($id,$title,$description,$path)
    {
        $this->db->where('id',$id); 

        $this->db->set('title',$title);
        $this->db->set('description',$description);
        if($path != 'none')
            $this->db->set('path',$path);
        $q1 = $this->db->where('albums');


        return $q1;   
    }

这是Controller方法

public function update2($action = "show",$id = 0 ,$row = 0 )
    {
        $data = array();
        if($action == "show"):
            $data['id'] = $id; 
            $data['row_number'] = $row; 
            $this->load->view('albums/edit_album',$data);
        else:

            $id = $this->input->post('id'); 
            $title = $this->input->post('title');
            $description = $this->input->post('description');
            $path  = $this->session->userdata('image_name_album');
            $this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');
            $this->form_validation->set_rules('description', 'Description', 'trim|required|xss_clean');


            if ($this->form_validation->run() == FALSE):
                $errors = array();
                $errors =  $this->form_validation->errors_array();
                $data['errors'] = $errors;
                $data['response'] = "Errors";
                echo json_encode($data);
                die();
            else:

                //update($id,$author,$title,$albums,$short,$lang,$status)
                $data['response'] = $this->albums_model->update($id,$title,$description,$path);; 
                $data['query'] = $this->albums_model->get($id); 
                print_r($data); 
                echo json_encode($data);
                die();
            endif;
        endif;



    }

1 个答案:

答案 0 :(得分:3)

你有一个迷你db-&gt;在你的更新函数中没有被调用的地方,所以它被添加到接下来运行的任何查询中。如果它应该遵循db-&gt;设置,则需要将它们都包含在括号{}

 $q1 = $this->db->where('albums');