doujiayuan8415 2016-04-29 18:56
浏览 27
已采纳

PHP数组的SQL语句

I am trying to refactor a switch statement to a collection. The switch statement selects the correct SQL query to run based on the switch conditional.

The queries all have concatenations within the switch statement, which does not work in an array within a class. Here is an example query similar to what I am dealing with:

"select qqqq,wwww,eeee,rrrr,tttt, 'yyyy' as uuuu from " . CONSTANT . ".aaaa as ssss inner join ' . CONSTANT2 . '.ddddd .... etc.

Any suggestions?

EDIT

Here is something close to the actual code. I made some changes for privacy:

 <?php

namespace Collections;

class ScheduleSQL {

private $sql;

public function __construct($plant)
{

    $sql = [
    'fgfghh' => "select fdgsgdfg, sgfdfgfg, rotary_priority,sfgfgfddg,sgfsgf, JB_ULT_CUST,jbr_status, jb_cust_want_date, routing_done, jb_date_need, js_time_ship, js_ship_via, jb_job_desc, rot.job_nbr, rot.job_plt, rot.job_type, sdfgdfgdf, js_ship_to, js_shipto_name, js_state, js_city, jbr_created as cvbvbv,xbvbvb, xbvbvb,xvbvbbcvb, vbvbvbas job_class, ruling_completed,scheduled_rule_date, converter, scenario_id, scenario_subdomain, transfer_type,transfer_job, jb_slope, jb_inch
                    from ' . DB_RASTER_LIB . 'jbraster as rot
                    inner join  ' . DB_STR_LIB . '.sessions_and_jobs as str
                        on rot.job_type = str.job_type and str.jb_job = rot.job_nbr and cgc.jb_plt = rot.job_plt
                    inner join ' . DB_RASTER_LIB . '.rot_job_sp as rot_sp
                        on rot_sp.jb_job = str.jb_job and rot_sp.jb_plt = str.jb_plt and rot_sp.job_type = rot.job_type
                    left outer join ' . DB_RASTER_LIB . '.job_data as rot_jd
                        on rot_jd.job_nbr = str.jb_job and rot_jd.job_plt = str.jb_plt and rot_jd.job_type = rot.job_type
                    inner join ' . DB_STR_LIB . '.jobshp as shp
                        on str.jb_job = shp.js_job and str.jb_plt = shp.js_plt and shp.job_type = rot.job_type and shp.js_primary = 1
                    where rot.job_plt = $plant and rot.job_type <> A and (jbr_status is null or jbr_status<>99)
                    order by jb_date_need asc"
];  
} 

public function getSQL($type)
{
    return $this->sql[$type];
}

}

  • 写回答

1条回答 默认 最新

  • douzhi1879 2016-05-08 11:34
    关注

    I found that the problem was in how the variable $plant is pulled into the query string.

    Including it within the double-quotes worked outside of the array - in the original context of a switch statement. However, when pulled into an array as I am doing, the variable has to be outside of the double-quotes.

    i.e.

    "query statement parts".$string."other query string parts"
    

    Thanks to all who responded.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加