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

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 Nov 26 '14 at 6:28
  • I mean something like addslash().... – vinrav Nov 26 '14 at 6:33
  • sometimes there will some symbols that i cant predict like''(single quotes).. then.. – vinrav Nov 26 '14 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,,


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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