rohitmb
rohitmb

Reputation: 151

Full text search MySql PHP

I have a column mysql table table_php_code, which contains column id & code_data. I need to search this table with the code that i have to get the Id.

e.g.

Now this code_data column may contain special characters(new line, extra spaces, single quote, double quote) as present in normal php code, stored in the table using addslashes php function.

Now if i have a sample code with all special characters how do i search it in this table. The simple select statement

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

doesn't seem to work always.

Please note the things i have tried are

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

Sample Code that needs to searched

<?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;

?>

Upvotes: 0

Views: 90

Answers (2)

Cl&#233;ment Malet
Cl&#233;ment Malet

Reputation: 5090

If it contains some special characters that block your query, you have to escape them :

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

This will escape every character following '\'

Upvotes: 1

Nebojsa Susic
Nebojsa Susic

Reputation: 1260

You can use

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

Upvotes: 0

Related Questions