简体   繁体   English

我如何在带有双引号的 php 查询中使用 MsSQL Where CONTAINS?

[英]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.我正在尝试在 PHP 中创建一个 SQL 查询,我在其中搜索包含带通配符的特定字符串的搜索结果的列。 I'm using CONTAINS for that and I've enabled Full Text-search on the MsSQL server.为此,我正在使用 CONTAINS,并且在 MsSQL 服务器上启用了全文搜索。 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 ("").在 MsSQL Management 中,我可以使用包含 function 的查询,但在 PHP 文件中,由于使用了双引号 (""),我无法使用它。

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 "').查询无法在 PHP 中运行,因为我在查询中使用的双引号包含 function ('" $query "')。

How could I escape these double quotes and be able to run this query inside a PHP file?我如何转义这些双引号并能够在 PHP 文件中运行此查询?

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'. SQLSTATE:42000 代码:102 消息:[Microsoft][SQL 服务器的 ODBC 驱动程序 17][SQL Server]“0”附近的语法不正确。

SOLUTION by @brombeer: @brombeer 的解决方案:

Had to change '"*$query*"' to '\"*$query*\"' , this helps escape the double quotes in PHP. So the query works properly now.必须将'"*$query*"'更改为'\"*$query*\"' ,这有助于转义 PHP 中的双引号。因此查询现在可以正常工作。

Working query:工作查询:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM