使用mysqli和fetch_object进行嵌套循环 [英] Nested looping with mysqli and fetch_object

查看:181
本文介绍了使用mysqli和fetch_object进行嵌套循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做两个表之间的比较。如果我找到一个匹配,我需要从一个表中复制数据并将其插入到另一个表中。我能够从表中拉出数据,但我遇到了我创建的嵌套循环的问题。它只循环并找到一个结果。在看表格时,有1000个匹配,所以我认为我做错了什么。我唯一的猜测是使用fetch_object和我如何使用它。我尝试使用fetch_assoc来代替,但是我得到了相同的结果:
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$''''''' ,pword)或死(无法连接到databse!);
mysqli_select_db($ mysqli,db_name)或者死(Could not Select Database!);

$ result = $ mysqli-> query('SELECT * FROM table_1');
$ title = $ mysqli-> query('SELECT title FROM table_2');
$ b $ if($ result& $ title){

while($ t_row = $ title-> fetch_object()){
$ b $ ($ row = $ result-> fetch_object()){

$ s_title = strtolower($ t_row-> title);
$ m_title = strtolower($ row-> description);
$ b $ if($ s_title == $ m_title){
echoMatched:$ s_title< strong> with< / strong> $ m_title< br />;
break;
}

}

}
}


你应该在var中存储第二个循环的结果。

  if($ result&& $ title)在第一次查看第二个游标之后, ){
$ results_rows = array();

while($ row = $ result-> fetch_object()){
$ results_rows [] = strtolower($ row-> description);


$ b while($ t_row = $ title-> fetch_object()){
$ s_title = strtolower($ t_row-> title);

if(in_array($ s_title,$ results_rows)){
echoMatched:$ s_title。 PHP_EOL;
}
}

}


I'm attempting to do a comparision between two tables. If I find a match, I need to copy data from one table and insert it into the other. I'm able to pull the data from the tables just fine, but I'm having issues with a nested loop I created. It only loops through and finds one result. When looking through the tables, there are 1000s of matches, so I assume I'm doing something wrong. My only guess is the use of fetch_object and how I'm using it. I attempted using fetch_assoc instead, but I get the same result:

$mysqli = mysqli_connect("127.0.0.1", "uname", "pword") or die("Can't connect to databse!");
mysqli_select_db($mysqli, "db_name") or die("Couldn't Select Database!");

$result = $mysqli->query('SELECT * FROM table_1');
$title = $mysqli->query('SELECT title FROM table_2');

if($result && $title){

   while($t_row = $title->fetch_object()){

      while($row = $result->fetch_object()){

         $s_title = strtolower($t_row->title);
         $m_title = strtolower($row->description);

         if($s_title == $m_title) {
            echo "Matched: $s_title <strong>with</strong> $m_title<br />";
            break;
         }

      }

   }
}

解决方案

You should store results of the second loop in var. Because after the first looking your second cursor is at the end of the result.

if($result && $title){
  $resulted_rows = array();

  while($row = $result->fetch_object()){
      $resulted_rows[] = strtolower($row->description);
  }


  while($t_row = $title->fetch_object()){
     $s_title = strtolower($t_row->title);

     if(in_array($s_title, $resulted_rows)) {
        echo "Matched: $s_title" . PHP_EOL;
     }
  }

}

这篇关于使用mysqli和fetch_object进行嵌套循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆