Page 1 of 1

Coping with backslashes when using sql queries

Posted: Sat Feb 02, 2008 2:41 pm
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?

Re: Coping with backslashes when using sql queries

Posted: Sat Feb 02, 2008 2:47 pm
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]

Re: Coping with backslashes when using sql queries

Posted: Sat Feb 02, 2008 2:51 pm
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.