我有一列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 '\';
这将转义'\'之后的每个字符
关于php - 全文搜索MySql PHP,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24697021/