0

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;
         }

      }

   }
}
csm232s
  • 1,660
  • 4
  • 32
  • 60

1 Answers1

0

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;
     }
  }

}
Andrej
  • 7,474
  • 1
  • 19
  • 21