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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度