title: PHP Mysqli SELECT, INSERT and UPDATE from different databases
body: <p>I am trying to select values from one DB. And insert and update the result into another. This is cronjob that needs to run everyday to replicate some data from one DB into another. I know I am missing steps / correct syntax, but I hope someone can help me out.    </p>
  
  <pre><code>&lt;?php
  
      $con_1=mysqli_connect("host","user","pw","db");
      // Check connection
      if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
      $con_2=mysqli_connect("host","user","pw","db");
      // Check connection
      if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
  
      $result = mysqli_query($con_1,"SELECT id, name FROM table GROUP BY 1,2");
  
      $mysqli-&gt;query($con_2, "INSERT INTO `table2`(`id`, `name`) VALUES ('".$result[1]."', ".$result[2].") 
      ON DUPLICATE KEY UPDATE name = ".$result[2]."");
  
      }
  
      mysqli_close($con_1);
      mysqli_close($con_2);
      ?&gt;
  </code></pre>