繁体   English   中英

全文搜索MySql PHP

[英]Full text search MySql PHP

我有一列mysql表table_php_code,其中包含列ID和code_data。 我需要使用我必须获取ID的代码搜索此表。

例如

  • id,code_data
  • 1,一些php代码
  • 2,一些php代码

现在,此code_data列可能包含特殊的字符(换行,多余的空格,单引号,双引号),如普通php代码中所示,并使用addslashes php函数存储在表中。

现在,如果我有一个包含所有特殊字符的示例代码,如何在此表中进行搜索。 简单的选择语句

select * from table_php_code where code_data = '<code_data_to_search>';

似乎并不总是工作。

请注意我尝试过的东西是

$query = "select * from table_php_code where REPLACE(code_data, '\r\n','')= ".addslashes(str_replace('\r\n', '', $code_data)).";";

需要搜索的示例代码

<?php

$userId=$_REQUEST["TARGET_USER_ID"];

$geolocation=$_REQUEST["TARGET_GEOLOCATION"];
$matches = preg_split("/[\s,]/", $geolocation);
$geolat=$matches[0];
$geolong=$matches[1];
$app_id="80";
$style="sban";

$max_width = $_REQUEST['max_image_width'];

if(empty($width) && (!empty($max_width)) && ($max_width > 539))  $width= '540';
if(empty($height) && (!empty($max_width)) && ($max_width > 539)) $height= '84';

if(empty($width) && (!empty($max_width)) && ($max_width > 459))  $width= '480';
if(empty($height) && (!empty($max_width)) && ($max_width > 459)) $height= '75';

if(empty($width) && (!empty($max_width)) && ($max_width > 299))  $width= '320';
if(empty($height) && (!empty($max_width)) && ($max_width > 299)) $height= '50';

if(empty($width) && (!empty($max_width)) && ($max_width > 239))  $width= '240';
if(empty($height) && (!empty($max_width)) && ($max_width > 239)) $height= '38';

if(empty($width))  $width= '320';
if(empty($height)) $height= '50';

$url="someurl".$userId."&_id=".$_id."&width=".$width."&height=".$height."&style=".$style."&lat=".$geolat."&long=".$geolong;

$info= '<script type="text/javascript" src="'.$url.'"></script>';

$info = '<meta name="viewport" content="somecontent, width=device-width, user-scalable=no" />' .$info;

echo $info;

?>

如果它包含阻止查询的某些特殊字符,则必须转义它们:

SELECT * 
FROM table 
WHERE field LIKE 'my \"text\"' ESCAPE '\';

这将转义'\\'之后的每个字符

您可以使用

select * 
from table_php_code 
where MATCH (code_data) AGAINST ('some text');

暂无
暂无

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

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