phpsql-serverwildcardcontainssqlsrv

How can i use MsSQL Where CONTAINS inside a php query with double quotes?


I'm trying to create a SQL query in PHP where I'm searching through the columns for search results containing a specific string with a wildcard. I'm using CONTAINS for that and I've enabled Full Text-search on the MsSQL server. Inside MsSQL Management, I can use the contains function with this query, but inside a PHP file, I can't use it because of the use of double quotes ("").

My query looks like this:

"SELECT * FROM table1 WHERE CONTAINS ((col1, col2, col3, col4, col5, col6), '"*$query*"')"

The query can't run in PHP because the double quote I'm using inside the queries CONTAINS function ('"$query"').

How could I escape these double quotes and be able to run this query inside a PHP file?

The only error I ran into and it doesn't really make anything clearer.

SQLSTATE: 42000 code: 102 message: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near '0'.


Solution

  • SOLUTION by @brombeer:

    Had to change '"*$query*"' to '\"*$query*\"', this helps escape the double quotes in PHP. So the query works properly now.

    Working query:

    "SELECT * FROM table1 WHERE CONTAINS ((col1, col2, col3, col4, col5, col6), '\"*$query*\"')";