ds3422222 2018-11-27 19:49
浏览 89
已采纳

循环用于使用php在mysql数据库中插入行

I have a for loop with php but is not working properly. Inside the for loop I have an "if" and an "else", but the loop stops iterate in the first "else" and should continue. Here is the code:

//counting the rows in database and the rows I want to insert

$total = count($rowToInsert); //for example 10 values
$totalDB = count($rowDB); // for example 5 values

// the for loop starts

for ($i=0; $i < $total; $i++){ //it should iterate until 10 values

  if(isset($rowDB[$i])){ //change the first 5 values

   $update = "UPDATE table SET name = '$name[$i]' WHERE ID = $rowToInsert[$i]";
   $result = mysqli_query($con, $update);

  } else { //it should iterate from sixth until tenth value

   $insert = "INSERT INTO table (name) VALUES ('$name[$i]')";
   $result = mysqli_query($con, $insert);

  // here is the next code

  $newTable = 'table'.$rowToInsert[$i];

  $newDB = "CREATE DATABASE $newTable CHARACTER SET utf8 COLLATE utf8_general_ci";
  $resultDB = mysqli_query($con, $newDB);

  // select the DB

  mysqli_select_db($con, $newTable) or die ("not found");

  } //end of else

} //end of for

The thing is if the database contain 5 rows and I want to insert, for example, 10 rows, the code works updating the first 5 with the new value, then it jumps to the "else" and starts to iterate in the sixth value and it works, but the next values doesn't.

Any idea what i'm doing wrong? Thanks!

Hector

  • 写回答

1条回答 默认 最新

  • dream890110 2018-11-27 23:22
    关注

    Ok I found the problem. In the else loop, when the iteration tries to select the database, for some reason it takes part of the name of the last iteration, so it couldn't find the database. The solution (maybe is not so clean) is connect and close the database connection in every iteration. The code stays like this:

    //counting the rows in database and the rows I want to insert
    
    $total = count($rowToInsert); //for example 10 values
    $totalDB = count($rowDB); // for example 5 values
    
    // the for loop starts
    
    for ($i=0; $i < $total; $i++){ //it should iterate until 10 values
    
      if(isset($rowDB[$i])){ //change the first 5 values
    
       $update = "UPDATE table SET name = '$name[$i]' WHERE ID = $rowToInsert[$i]";
       $result = mysqli_query($con, $update);
    
      } else { //it should iterate from sixth until tenth value
    
       // reconnect to db
    
       $con = mysqli_connect($host, $user, $pass) or die ("unable to connect");
       $db = "database";
    
       $insert = "INSERT INTO table (name) VALUES ('$name[$i]')";
       $result = mysqli_query($con, $insert);
    
      // here is the next code
    
      $newTable = 'table'.$rowToInsert[$i];
    
      $newDB = "CREATE DATABASE $newTable CHARACTER SET utf8 COLLATE utf8_general_ci";
      $resultDB = mysqli_query($con, $newDB);
    
      // select the DB
    
      mysqli_select_db($con, $newTable) or die ("not found");
    
      //close the connection to db;
    
      $con->close();
    
      } //end of else
    
    } //end of for
    

    Thanks you all for inspiring the answer!

    Hector

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口