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?)";
}
}
?>
Please advise. Any suggestion is more than welcome.
Thank you all.
</div>