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 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM