Coping with backslashes when using sql queries

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PhpDog
Forum Commoner
Posts: 58
Joined: Mon Jan 14, 2008 10:23 am

Coping with backslashes when using sql queries

Post by PhpDog »

The sql table I have to work with contains backslashes in the column names. My current sql query:

$sql = "

SELECT

Mortgage_Number,
Sequence_Number,
Valuation_Date,
Open_Market_Value,
Reinstatement_Value,
Third_Party_Transaction_Type,
Asking_Price,
Forced_Sale_Price,
Property_Categpry,
Cracks_Y/N,
Underpinned_Y/N,
Adverse_Sales_Features_Y/N,
Reports_Required_Y/N,
Unfavorable_Y/N,
Added_By_User,
Added_Date,
Amend_By_User,
Amend_Date

FROM valuations

WHERE (Mortgage_Number = '$mortgage')

ORDER BY $criteria $direction

";

throws errors "Warning: mssql_query() [function.mssql-query]: message: Invalid column name 'Cracks_Y'. (severity 16) in ..."

How can get around this please?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Coping with backslashes when using sql queries

Post by alex.barylski »

Don't use slashes in the first place.

You could maybe try the backtick operator around the column names.

[sql]SELECT * FROM TABLE WHERE `column` = 2[/sql]
PhpDog
Forum Commoner
Posts: 58
Joined: Mon Jan 14, 2008 10:23 am

Re: Coping with backslashes when using sql queries

Post by PhpDog »

Hockey wrote:Don't use slashes in the first place.

You could maybe try the backtick operator around the column names.

[sql]SELECT * FROM TABLE WHERE `column` = 2[/sql]
Unfortuantely, I don't have a say in the column names as the table belongs to my boss.

However, I've just found that putting square brackets around the column names containing the back slashes solved the problem.

Thanks for replying.
Post Reply