使用包含特殊符号的wpdb选择行

时间:2014-11-26 作者:vinrav

我得到一张表,其中一列有特殊字符。我在表格中插入为

$sql=$wpdb->prepare( 
         "INSERT INTO `mytable`
            ( `question`, `remarks`)
        VALUES (\'%s\', \'%s\' )", 
        array(sanitize_text_field($question),sanitize_text_field($remarks)) );
$wpdb->query($sql);
然后又插入了这样一行

-------------------------------------------------|
| question                          |   remarks  |
-------------------------------------------------|
| \\"creating world \\" written by?   |   chom-lu  |
|                                   |            |
-------------------------------------------------|
但当我尝试选择

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

echo $sql; //reutrns as
SELECT * FROM `mytable` WHERE `question` LIKE \'%\\"creating world \\" written by?%\'
我做错了什么。。请帮助找到解决此问题的方法。。这个问题从早上就把我的一整天都耗光了。。。

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

3 个回复
最合适的回答,由SO网友:vinrav 整理而成

伙计们,我想出了一个办法。。。。虽然不干净,但它能帮我完成任务。。

global $wpdb;
$sql=$wpdb->prepare("SELECT * FROM mytable WHERE question =\'%s\'",         
                      like_escape(sanitize_text_field($question)));
/*******************
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
}

SO网友:Domain

您可以尝试以下查询

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

global $wpdb;
$quest=addcslashes(\'\\"creating world \\" written by?\',\'\\"?\');
$result = $wpdb->get_results("SELECT * FROM mytable WHERE
            question =\'{$quest}\'");

SO网友:Seenuvasan Velayutham

早上好,试试这个解决方案,,

SELECT * FROM mytable WHERE 
question LIKE \'%[\\"creating world \\" written by?]%\'
使用方括号,,,我希望这对你有帮助,,

谢谢

结束