duanchu3376 2016-07-15 10:59
浏览 37
已采纳

在PHP中的MS SQL查询中转义$字符[重复]

This question already has an answer here:

I have a situation under which I have to use $ (dollar sign) inside a MS SQL query since the table is named this way (e.g. [My company name$Fixed Asset].[Description]), but PHP recognizes part "$Fixed" as a variable and then later on when I try to execute the code in the browser it throws an error e.g. "Notice: Undefined variable: Fixed".

How do I escape this character?

Here is the part of the code I am trying to use:

    <?php
    if (isset($_POST['submit'])) {
        if ($_POST['password'] === 'mypass') {

            $result = mssql_query("SET ANSI_NULLS ON;");
            $result1 = mssql_query("SET ANSI_WARNINGS ON;"); 
            $query = mssql_query("

TRUNCATE TABLE [fixed_assets].[dbo].[fa_masterdata];

INSERT INTO [fixed_assets].[dbo].[fa_masterdata]

SELECT  
[My company name$Fixed Asset].[No]
,[My company name$Fixed Asset].[Description]
,[Search Description]
,[Description 2]
,[Location Code]
,[Vendor No_]
,[My company name$FA Depreciation Book].[Disposal Date]
,[My company name$FA Location].[Name]
,[My company name$Comment Line].Code
,[My company name$Comment Line].Comment
FROM [SQL SERVER NAME\INSTANCE].[MY COMPANY].[dbo].[My company name$Fixed Asset]


full outer join [SQL SERVER NAME\INSTANCE].[MY COMPANY].[dbo].[My company name$FA Location]
on [My company name$Fixed Asset].[FA Location Code] = [My company name$FA Location].[Code]

full outer join [SQL SERVER NAME\INSTANCE].[MY COMPANY].[dbo].[Record Link]
on [My company name$Fixed Asset].[No_] = convert(varchar(100),SUBSTRING([Record Link].[Record ID],8,8))

full outer join [SQL SERVER NAME\INSTANCE].[MY COMPANY].[dbo].[My company name$Comment Line]
on [My company name$Comment Line].[No_] = [My company name$Fixed Asset].[No_]

full outer join [SQL SERVER NAME\INSTANCE].[MY COMPANY].[dbo].[My company name$FA Depreciation Book]
on [My company name$FA Depreciation Book].[Fa No_] = [My company name$Fixed Asset].[No_]

where [My company name$Fixed Asset].[No_] != null");

    echo "<br/><br/><div class='alert alert-success col-sm-offset-5 col-sm-2'><span>Fixed assets data imported from NAV successfully!!</span></div>";
            } else {
                echo "<br/><br/><span>Import failed! (wrong password entered?)";
            }
    }
    ?>

ERROR SAMPLE

Please advise. Any suggestion is more than welcome.
Thank you all.

</div>
  • 写回答

1条回答 默认 最新

  • drdr123456 2016-07-15 11:07
    关注

    You can escape the $ and then it won't be read as a variable by PHP.

    echo "\$test";
    

    Demo: https://eval.in/605867 Vs. https://eval.in/605866 (which is empty because $test doesnt exist)

    ...

    or since your query doesn't use single quotes just use single quotes for the string encapsulation.

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

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算