2017-04-08 66 views
0

我有一件奇怪的事情,我不知道失败在哪里。Wordpress get_results解析符号“&”

$name = "Fast & Furious 8"; 

$res1 = $wpdb->prepare(
    " 
     SELECT 
      * 
     FROM 
      wp_dbtable 
     WHERE 
      filmname = '%s' 
     LIMIT 1 
    ", 
    $name 
); 

$res = $wpdb->get_results($res1); 

foreach($res as $reseachG) { 

} 

我有一个问题符号&。由于某些原因,它不会从我的桌子上拉出任何东西,即使它应该这样做。

如果我使用的变量,而不是文本它的自我,就像这样:

$res1 = $wpdb->prepare(
    " 
     SELECT 
      * 
     FROM 
      wp_dbtable 
     WHERE 
      filmname = '%s' 
     LIMIT 1 
    ", 
    "Fast & Furious 8" 
); 

它的工作原理。此外,变量内的其他文本运行良好。

所以看起来prepareget_results不接受这个符号,或改变它。我该如何解决它?我无法在互联网上找到任何提示。

它可能不是PHP-问题或My-SQL-问题,它可能是WordPress类的问题。

非常感谢。

+0

'echo“的输出是什么

"; print_r($res1); echo "
”;'?另外,在你的查询中你有''%s''。你不需要那个。 '%s'将自动用变量包含变量名称。 –

+0

我在自定义表格中添加了一个名为'Fast&Furious 8'的条目,并且在运行第一个代码以查找该字符串时没有任何问题。 – RRikesh

+0

@MerianosNikos the Outpu是: SELECT * FROM wp_dbtable WHERE filmname ='Fast&Furious 8'LIMIT 1 –

回答

0

上面的代码是正确的。问题是,我从数据库中提取了电影名称的文本。他将&转换为html特殊字符。所以我必须运行:htmlspecialchars_decode($filmname);。因为每种印刷方法都印刷正确的文字,所以不容易找到。

我在互联网上找到了一个函数,它显示了我隐藏的字符和每一个真正的entiti。也许这会帮助你们中的一些也:

function hexdump ($data, $htmloutput = true, $uppercase = false, $return = false){ 
// Init 
$hexi = ''; 
$ascii = ''; 
$dump = ($htmloutput === true) ? '<pre>' : ''; 
$offset = 0; 
$len = strlen($data); 

// Upper or lower case hexadecimal 
$x = ($uppercase === false) ? 'x' : 'X'; 

// Iterate string 
for ($i = $j = 0; $i < $len; $i++) 
{ 
    // Convert to hexidecimal 
    $hexi .= sprintf("%02$x ", ord($data[$i])); 

    // Replace non-viewable bytes with '.' 
    if (ord($data[$i]) >= 32) { 
     $ascii .= ($htmloutput === true) ? 
         htmlentities($data[$i]) : 
         $data[$i]; 
    } else { 
     $ascii .= '.'; 
    } 

    // Add extra column spacing 
    if ($j === 7) { 
     $hexi .= ' '; 
     $ascii .= ' '; 
    } 

    // Add row 
    if (++$j === 16 || $i === $len - 1) { 
     // Join the hexi/ascii output 
     $dump .= sprintf("%04$x %-49s %s", $offset, $hexi, $ascii); 

     // Reset vars 
     $hexi = $ascii = ''; 
     $offset += 16; 
     $j  = 0; 

     // Add newline    
     if ($i !== $len - 1) { 
      $dump .= "\n"; 
     } 
    } 
} 

// Finish dump 
$dump .= $htmloutput === true ? 
      '</pre>' : 
      ''; 
$dump .= "\n"; 

// Output method 
if ($return === false) { 
    echo $dump; 
} else { 
    return $dump; 
} 
} 

您展示了六码的每个字符,并每一个空间,线,特殊符号trasformed或线制动器。它有助于查看真实的数据。

thx to all。