无法让我的SQL查询与php一起使用

时间:2014-06-26 08:15:01

标签: php sql oracle

我正在使用查询来组织数据并且它与ORACLE SQL DEVELOPER一起运行良好,但是当我尝试在我的PHP脚本中获取它时,它会返回错误。


我的php文件:

<?php
    include 'connexion.php';
    //


    $sql  = oci_parse($conn, 'select c.* from ( 
    select 
    b.EB_DOMAINE_NOM,b.EB_INDICATEUR_LIBELLE,b.EB_RESULTAT_VALEUR as EB_RESULTAT_VALEUR_m,
    b.EB_RESULTAT_INSERT_DATE as EB_RESULTAT_INSERT_DATE_m,
    b.EB_INDICATEUR_NUM, 
    EB_RESULTAT_DTM.VALEUR as valeur_m1,(EB_RESULTAT_DTM.VALEUR-b.EB_RESULTAT_VALEUR) as diff_m1_m,
    rank() over (partition by EB_RESULTAT_DTM.X_EB_INDICATEUR_DTM_NUM, to_char(EB_RESULTAT_DTM.INSERT_DATE,'yyyymm') 
    order by to_char(EB_RESULTAT_DTM.INSERT_DATE,'yyyymmdd') asc) as rang
    from (
    select A.*  
    from (
    SELECT EB_DOMAINE_DTM."NOM" AS EB_DOMAINE_NOM,
             EB_INDICATEUR_DTM."LIBELLE" AS EB_INDICATEUR_LIBELLE,
             EB_RESULTAT_DTM."VALEUR" AS EB_RESULTAT_VALEUR, 
             EB_RESULTAT_DTM."INSERT_DATE" AS EB_RESULTAT_INSERT_DATE,
             EB_INDICATEUR_DTM."INDICATEUR_NUM" AS EB_INDICATEUR_NUM,
             rank() over (partition by EB_INDICATEUR_DTM.INDICATEUR_NUM,to_char(EB_RESULTAT_DTM.INSERT_DATE,'yyyymm') order by to_char(EB_RESULTAT_DTM.INSERT_DATE,'yyyymmdd') asc) as rang,
             to_char(EB_RESULTAT_DTM.INSERT_DATE,'yyyymm') as MOIS_M
        FROM "COMPTEUR_OWNER"."EB_DOMAINE_DTM" EB_DOMAINE_DTM
             INNER JOIN
             "COMPTEUR_OWNER"."EB_INDICATEUR_DTM" EB_INDICATEUR_DTM
                ON EB_DOMAINE_DTM."EB_DOMAINE_DTM_NUM" =
                      EB_INDICATEUR_DTM."X_EB_DOMAINE_DTM_NUM"
             INNER JOIN
             "COMPTEUR_OWNER"."EB_RESULTAT_DTM" EB_RESULTAT_DTM
                ON EB_INDICATEUR_DTM."INDICATEUR_NUM" =
                      EB_RESULTAT_DTM."X_EB_INDICATEUR_DTM_NUM"
       WHERE     EB_INDICATEUR_DTM."INDICATEUR_NUM" = 106
             AND EB_RESULTAT_DTM."INSERT_DATE" BETWEEN ADD_MONTHS (SYSDATE, -9) 
                                                   AND SYSDATE
    ) a where a.rang=1
    ) b left outer join COMPTEUR_OWNER.EB_RESULTAT_DTM EB_RESULTAT_DTM 
    on EB_RESULTAT_DTM.X_EB_INDICATEUR_DTM_NUM =b.EB_INDICATEUR_NUM 
    AND TO_CHAR(EB_RESULTAT_DTM.INSERT_DATE,'YYYYMM')=TO_CHAR(ADD_MONTHS(TO_DATE(b.MOIS_M,'YYYYMM'),1),'YYYYMM') 
    ) c where c.rang=1
    order by TO_CHAR(c.EB_RESULTAT_INSERT_DATE_m,'YYYYMM') asc

    ');

    oci_execute($sql);

?&gt;

我因'YYYYMM'而收到错误。或者,如果我修复它,我会收到A.RANG等错误。


在Oracle中,我得到了我想要的东西:

http://i.imgur.com/2snvnWu.png


有没有办法获得与Oracle相同的结果?

1 个答案:

答案 0 :(得分:0)

您在查询中使用单引号。单引号是什么打破PHP。 n00b错误。您所要做的就是用双引号括起您的sql语句,而不是单引号。或者用\

转义单引号