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

在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';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化