dongzhi8487 2019-08-06 06: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-06 07: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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀