dpgjci27392
2014-06-26 08:15 阅读 36

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

I am using a query to get data organized and it works well with ORACLE SQL DEVELOPER, though when I am trying to get it in my PHP script, it returns an error.


My php file:

<?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);

?>

I get an error because of the 'YYYYMM'. Or if I fix it I get an error with A.RANG etc..


While in Oracle I get what I want:

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


Is there a way to get the same result as in Oracle?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    duanshang3230 duanshang3230 2014-06-26 08:18

    You are using single quotes in your query. the single quotes is what is breaking php. n00b mistake. All you have to do is wrap your sql statement in double quotes, not single quotes. Or alternatively escape the single quote with a \

    点赞 评论 复制链接分享

相关推荐