I got a table with one of the column have special charaters. I inserted to the table as

         "INSERT INTO `mytable`
            ( `question`, `remarks`)
        VALUES ('%s', '%s' )", 
        array(sanitize_text_field($question),sanitize_text_field($remarks)) );

which in turn inserted a row like this

| question                          |   remarks  |
| \"creating world \" written by?   |   chom-lu  |
|                                   |            |

But When i try to SELECT

$quest='"creating world " written by?';
$sql=$wpdb->prepare("SELECT * FROM `mytable` WHERE
            `question` LIKE '%s' AND `remarks` LIKE '%s'" ,
$result = $wpdb->get_results($sql);

return empty rows..

echo $sql; //reutrns as
SELECT * FROM `mytable` WHERE `question` LIKE '%\"creating world \" written by?%'

What i did wrong.. please help to find a way out of this problem.. this problem eating up my whole day from morning...

will you Speak in PHP... plezzzzzzzzze and IN GENERALIZED FORMAT

3 Answers 3


You can try the following query

SELECT * FROM `mytable` WHERE `question` = '\\\"creating world \\\" written by\?'

In php

global $wpdb;
$quest=addcslashes('\"creating world \" written by?','\"?');
$result = $wpdb->get_results("SELECT * FROM mytable WHERE
            question ='{$quest}'");
  • That works for single line selection.. will you speak in PHP.. PLSSSSSSSS
    – vinrav
    Commented Nov 26, 2014 at 6:28
  • I mean something like addslash()....
    – vinrav
    Commented Nov 26, 2014 at 6:33
  • sometimes there will some symbols that i cant predict like''(single quotes).. then..
    – vinrav
    Commented Nov 26, 2014 at 10:34

Guys i figured out a way to do this.... Not a clean one but it just do the job for me..

global $wpdb;
$sql=$wpdb->prepare("SELECT * FROM mytable WHERE question ='%s'",         
actualy following do the trick for me
all SINGLE \ are replaced by \\\ 
where SINGLE slashed statement genereated by prepare()
$sql=str_replace('\\', '\\\\\\', $sql); 
$result = $wpdb->get_results($sql);
if (empty($result))
    //doing some stuff

very good morning, try this solution,,

question LIKE '%[\"creating world \" written by?]%'

use square bracket,,, i hope this will help you,,


  • that is not helping at all while WisdmLabs solution worked for me.. but i need something in php to generate something like that...
    – vinrav
    Commented Nov 26, 2014 at 6:32
  • Please refer this link , i hope this link will help you,, (sqlserver2000.databases.aspfaq.com/…) Commented Nov 26, 2014 at 6:36
  • its a dead end.. shows an " An error occured...Exception of type 'System.Web.HttpUnhandledException' was thrown."
    – vinrav
    Commented Nov 26, 2014 at 6:38
  • sry , check it now,, (sqlserver2000.databases.aspfaq.com/…) Commented Nov 26, 2014 at 6:40
  • sory yar.. it may be our problem... we use mysql ...
    – vinrav
    Commented Nov 26, 2014 at 6:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.