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

How to call or show table that have column name contains bracket in php?

我无法调用包含方括号 () 的列的 table。

如何调用 table 以及是否可以为列名起别名?

包含方括号的 table 是 material detail 列是 Panjang(mm) 和 Lebar(mm)

我的控制器:

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);
    }
}

我的模特:

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();
}

我的 Ajax 在控制台中调用响应:

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');
            }
        });

    }

我希望输出是成功响应,但响应显示错误

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

就像我在评论中多次说过的那样,在那些包含破折号、空格、括号等的列名上使用反引号作为标识符,以便您可以有效查询。

将它与 ->select 方法上的 false 标志结合起来,以便它允许您使用原始输入并允许那些反引号。

$this->db->select('
    `mmaterial.Lebar(mm)`,
    `mmaterial.Panjang(mm)`
', false);