dongzhi8487 2019-08-05 22:50
浏览 75
已采纳

如何在php中调用或显示列名包含括号的表?

I can't call the table that has column contains brackets ().

How do I call the table and can I aliases the column name?

The table that contains brackets is material detail and the column is Panjang(mm) and Lebar(mm)

My Controller :

public function GetDataID()
{

    $id = $this->input->post('id');
    $cek = $this->list->getDetailBagJob2($id);
    if ($cek->num_rows() == 0) {
        $bagianjob = $this->list->getDetailBagJob1($id)->result();
        echo json_encode($bagianjob);
    } else {
        $bagianjob = $this->list->getDetailBagJob2($id)->result();
        echo json_encode($bagianjob);
    }
}

My Models :

public function getDetailBagJob1($id)
{

    $this->db->from('opdetailbagianjob');
    $this->db->join('mbagianjob', 'opdetailbagianjob.ID_Bagian_Job=mbagianjob.ID_Bagian_Job');
    $this->db->join('mmaterial', 'opdetailbagianjob.ID_Gramatur=mmaterial.ID_Material');
    $this->db->join('mmaterial_type', 'opdetailbagianjob.ID_Bahan=mmaterial_type.ID_Mat_Type');
    $this->db->join('mmaterialdetail', 'opdetailbagianjob.ID_Ukuran=mmaterialdetail.ID_Mat_Detail');

    $this->db->where('No_Enquiry', $id);

    return $this->db->get();
}
public function getDetailBagJob2($id)
{

    $this->db->select('opdetailbagianjob.*,opdetailbagianjob.Keterangan as keteranganbagjob,mbagianjob.*,mmaterial.*,mmaterial_type.*,mmaterialdetail.*');
    $this->db->from('opdetailbagianjob');
    $this->db->join('mbagianjob', 'opdetailbagianjob.ID_Bagian_Job=mbagianjob.ID_Bagian_Job');
    $this->db->join('mmaterial', 'opdetailbagianjob.ID_Gramatur=mmaterial.ID_Material');
    $this->db->join('mmaterial_type', 'opdetailbagianjob.ID_Bahan=mmaterial_type.ID_Mat_Type');
    $this->db->where('No_Enquiry', $id);

    return $this->db->get();
}

My Ajax To call the response in console :

function ambilenquiry(id) {

        $.ajax({
            url: "<?php echo base_url(); ?>Enquiry/GetDataID",
            type: "POST",
            dataType: "json",
            data: {
                "id": id
            },
            success: function(data) {
                console.log(data);
            },
            error: function(data) {
                alert('Gagal');
            }
        });

    }

I expect the output is success response, but the response show error with

jquery-3.4.1.min.js:2 POST http://10.3.1.10:81/gapcalc/Enquiry/GetDataID 500 (Internal Server Error)

展开全部

  • 写回答

1条回答 默认 最新

  • doulianglou0898 2019-08-05 23:38
    关注

    Like what I have said in the comments many times, use backticks for identifiers on those column names that contain dashes, spaces, parenthesis and the like, so that you can have a valid query.

    Combine it with the false flag on the ->select method so that it allows you to use raw input and allow those backticks.

    $this->db->select('
        `mmaterial.Lebar(mm)`,
        `mmaterial.Panjang(mm)`
    ', false);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部