简体   繁体   English

Wordpress $ wpdb-> get_results(…)问题与比较字符串

[英]Wordpress $wpdb->get_results(…) issues with comparing strings

I am trying to iterate through the rows in a phpbb table called phpbb_posts and extract each entry in phpbb's "post_subject" column and compare its value with a predefined string in Wordpress PHP file but I am having some issues - the expressions don't evaluate to true. 我试图遍历称为phpbb_posts的phpbb表中的行,并提取phpbb的“ post_subject”列中的每个条目,并将其值与Wordpress PHP文件中的预定义字符串进行比较,但是我遇到了一些问题-表达式的计算结果不等于真正。

My phpBB's tables are installed in WP's database so I have full access to the values. 我的phpBB表已安装在WP的数据库中,因此我可以完全访问这些值。

See the code below to demonstrate the issue I am having. 请参阅下面的代码以演示我遇到的问题。

function matchPhpBBTopic()
{
  global $wpdb; 

  $wp_post_title_string = get_the_title();

  $result = $wpdb->get_results("SELECT * FROM phpbb_posts"); 

  foreach($result as $row)
  {      
    $phpbb_post_title_array   = array($row->post_subject);  
    $phpbb_post_title_string  = implode("", $phpbb_post_title_array);

    // One of the values in $row->post_subject contains
    // the value in $wp_post_title_string
    if (strcmp($wp_post_title_string, $phpbb_post_title_string) == 0)
    {
      // This line never runs but the $wp_post_title_string value
      // is there, in the table, I've printed it and it's there
      echo 'We found a match!<br>';
    }                                                                              
  }  
}

Any assistance would be appreciated. 任何援助将不胜感激。

So in other words, I have a topic posted in WP and I have exactly the same topic posted in phpBB and I want to iterate through the phpBB's table and when I find the topic, I want to run some code. 因此,换句话说,我曾经参与过WP的话题,我有完全一样的题目张贴在phpBB和我想通过的phpBB的表进行迭代,当我发现这个话题,我想运行一些代码。 I don't understand why the "if" expression does not run. 我不明白为什么“ if”表达式不能运行。

Couldn't you just do: 你不能只是做:

if ($wp_post_title_string == $phpbb_post_title_string) {} 如果($ wp_post_title_string == $ phpbb_post_title_string){}

I don't think strcmp() is appropriate. 我认为strcmp()不适合。 It converts the string to encoding numbers. 它将字符串转换为编码数字。

http://us1.php.net/strcmp http://us1.php.net/strcmp

Also check for lower and upper case, spaces, and different encodings. 还要检查大小写,空格和不同的编码。

Do strtolower() and trim() first and see what you get. 首先执行strtolower()和trim(),看看会得到什么。

Also looks like you're imploding subject and title, so don't think they'll match. 而且看起来您正在逼迫主题和标题,所以不要认为它们会匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM