title: PHP array iteration in loop and put fetched results in a table with MySQL
body: <p><br>
  <br>I am a beginner and read quite many threads here before posting as whatever i tried, could not do what i need to do.. so here's what I do:
  <br>1. Run a mysql query to DB by sorting in ASC and DESC mode and get 20 records of each type, I want to take 3 features: term, termid and currentRank. 
  <br>2. Then I fetch these with mysqli_fetch_array
  <br>3. Then I need these 20 results to be stored in a temporary table, I suppose with some sort of loop. 
  <br><br>So far i was able to iterate over the values and print them, but I am having trouble with running a query within the While loop to input the current values in the temporary table. Here my PHP for this part of the application:</p>
  
  <pre><code>&lt;?php
  $conn=mysqli_connect("localhost","root");
  $db_select=mysqli_select_db($conn,"irdb");
  
  //select a random logo from db
  $query = mysqli_query($conn,"SELECT logo, companyid FROM company ORDER BY RAND() LIMIT 1");   
  
  $row = mysqli_fetch_array($query);
  $image =  mysqli_real_escape_string($conn,$row[0]); 
  $compID = mysqli_real_escape_string($conn,$row[1]);
  
  // create new term temporary table newtermlist
  $newtermlist = mysqli_query($conn,"CREATE TABLE IF NOT EXISTS irdb.newtermlist (
              `newtermid` INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
              `origtermid` INT(10),
              `companyid` INT(20),
              `term` VARCHAR(50),
              `currentRank` decimal(50,0))");
  
  // sort terms for the random company by highest ranked values
  $highestRankTerms = mysqli_query($conn, "SELECT term, currentRank, termid FROM ranks_test 
  WHERE companyID='$compID' 
  ORDER BY currentRank DESC 
  Limit 20");
  
  $highestRow = mysqli_fetch_array($highestRankTerms);        
  $highestTerm =  mysqli_real_escape_string($conn,$highestRow[0]); 
  $highestCurrentRank = mysqli_real_escape_string($conn,$highestRow[1]);
  $highestTermID = mysqli_real_escape_string($conn,$highestRow[2]);
  
  while ($row = mysqli_fetch_array($highestRankTerms,MYSQL_ASSOC)){
        printf("&lt;br&gt; term: %s | currentRank: %s | termid: %s", $row["term"], $row["currentRank"], $row["termid"]);
        // I need to RUN the UPDATE QUERY below:
        // $insertInNewtermlist = mysqli_query($conn,"INSERT INTO `irdb`.`newtermlist` VALUES ('','$compID','$highestTerm','$highestCurrentRank')");
  }
  
  ?&gt;
  </code></pre>
  
  <p><strong>When I print I get the following:</strong> <br><br>
  <a href="https://i.stack.imgur.com/bqYuR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bqYuR.png" alt="enter image description here"></a></p>
  
  <p>So it works partially.. <strong>The strange thing is that sometimes when I try different things, it gives me NO error, but no record is present in the table or at most two terms out of 20.</strong> </p>
  
  <p>I would really appreciate help on it, cause i tried everything that I am capable of, but considering the fact that I am self-studying, I probably do not know something that might help. </p>
  
  <p>Thanks, 
  Ani</p>