PHP PDO插入语句超过执行时间

时间:2017-04-10 05:39:38

标签: php mysql pdo

我使用预处理语句插入批量值,分配值并在循环中执行仍然需要太长时间并且超出执行限制。

这是我的代码:

if(isset($_GET['mov_id']) && isset($_GET['season']))
{
$id= $_GET['mov_id'];
$season = $_GET['season'];
$stmt = $DB_con->prepare("SELECT * FROM `mov_movies` where moviesID =:id");
$stmt->execute(array(':id'=>$id));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
$imdbID = $row['IMDBid'];


if(isset($_POST['btn-addmovie']))
{
$episodes = trim($_POST['tepisodes']);

    $name = "";
     $episode = "";
     $DOR = "";
     $IMDB = "";
     $drive = "";
     $cdn = "";

     $stmt = $DB_con->prepare("DELETE FROM `mov_season` where movId =:id");
     $stmt->execute(array(':id'=>$id));
     $count = 1;

    $stmt = $DB_con->prepare("INSERT INTO `mov_season` (`movId`, `season`, `episode`, `releaseDate`, `IMDB`, `driveLink`, `cdnLink` , `title`) VALUES (:id, :season, :episode,:releaseDate, :IMDB,:drive,:cdn,:name)");
    $stmt->bindparam(":id", $id);
       $stmt->bindparam(":season", $season);
       $stmt->bindparam(":episode", $episode);  
       $stmt->bindparam(":releaseDate", $DOR);  
       $stmt->bindparam(":IMDB", $IMDB ); 
       $stmt->bindparam(":drive", $drive ); 
       $stmt->bindparam(":cdn", $cdn );
       $stmt->bindparam(":name", $name);
     while($count < $episodes){

     $name = trim($_POST['name'.$count]);
     $episode = trim($_POST['episode'.$count]);
     $DOR = trim($_POST['DOR'.$count]);
     $IMDB = trim($_POST['imdb'.$count]);
     $drive = trim($_POST['drive'.$count]);
     $cdn = trim($_POST['cdn'.$count]);

     if(($drive != "") || ($cdn = "")){



       $stmt->execute();

       $count++;
     }




    }
     $success = "Successfully Updated!";



 }
 }

声明发生在if循环内部循环中。即使只执行一次,它也会超过120秒的执行时间。

2 个答案:

答案 0 :(得分:0)

尝试使用批量插入。在这里你必须执行单个查询,这将插入多个记录

PDO Prepared Inserts multiple rows in single query

答案 1 :(得分:0)

这是你的问题。如果这个条件为真,你只需递增$ count,这意味着你可能处于无限循环中。

if(($drive != "") || ($cdn = "")){
    $stmt->execute();
    $count++;
}

改为执行此操作,因此$ count总是递增。

if(($drive != "") || ($cdn = "")){
    $stmt->execute();
}
$count++;