PHP页面在MySQL INSERT期间停止加载

时间:2014-08-07 11:29:19

标签: php mysql sql

我有这个功能:

function addSteamProduct($id, $type = "app"){
  global $connection;
  $result = mysqli_query($connection, "SELECT 1 FROM products WHERE sid = '".$id."' AND ".($type == "app" ? "(p_type = 'game' OR p_type = 'dlc')" : "p_type = 'bundle'").";");
  $data = mysqli_fetch_array($result, MYSQLI_ASSOC);
  if($data != null)
    return "001";
  if($type == "app"){
    $json = file_get_contents("http://store.steampowered.com/api/appdetails/?cc=it&l=english&appids=".$id);
    $json = str_replace(array("<h1>", "<h2>", '<h1 class=\"bb_tag\">', '<h2 class=\"bb_tag\">'), '<div style=\"font-weight:bold\">', $json);
    $json = str_replace(array("<\/h1>", "<\/h2>"), "<\/div>", $json);
    $json = str_replace(array("<p>", "<\/p>"), array("<div>", "<\/div>"), $json);
  }
  else if($type == "sub")
    $json = file_get_contents("http://store.steampowered.com/api/packagedetails/?cc=it&l=english&packageids=".$id);
  $json = str_replace(array("\u00ae", "\u2122", "http:\/\/steamcommunity.com\/linkfilter\/?url=", "https:\/\/steamcommunity.com\/linkfilter\/?url="), "", $json);
  $json = json_decode($json, true);
  if(!$json[$id]["success"])
    return "001";
  else{
    $json = $json[$id]["data"];
    if(!in_array($json["type"], array("game", "dlc")) && $type != "sub")
      return "001";
    if(sizeof($json["price_overview"]) <= 0 && sizeof($json["price"]) <= 0)
      return "001";
    if($type == "sub"){
      for($i = 0; $i < sizeof($json["apps"]); $i++){
        $ids .= ($i == 0 ? "" : ",").$json["apps"][$i]["id"];
      }
      $json2 = file_get_contents("http://store.steampowered.com/api/appdetails/?cc=it&l=english&appids=".$ids);
      $json2 = str_replace(array("\u00ae", "\u2122", "http:\/\/steamcommunity.com\/linkfilter\/?url=", "https:\/\/steamcommunity.com\/linkfilter\/?url="), "", $json2);
      $json2 = json_decode($json2, true);
      while($json2[key($json2)]){
        for($i = 0; $i < sizeof($json2[key($json2)]["data"]["categories"]); $i++){
          if(!arrayKeySearch("id", $json2[key($json2)]["data"]["categories"][$i]["id"], $json["categories"]))
            $json["categories"][] = $json2[key($json2)]["data"]["categories"][$i];
        }
        for($i = 0; $i < sizeof($json2[key($json2)]["data"]["genres"]); $i++){
          if(!arrayKeySearch("id", $json2[key($json2)]["data"]["genres"][$i]["id"], $json["genres"]))
            $json["genres"][] = $json2[key($json2)]["data"]["genres"][$i];
        }
        next($json2);
      }
      reset($json2);
      $json["developers"] = $json2[key($json2)]["data"]["developers"];
      $json["publishers"] = $json2[key($json2)]["data"]["publishers"];
      $json["supported_languages"] = $json2[key($json2)]["data"]["supported_languages"];
      $json["screenshots"] = $json2[key($json2)]["data"]["screenshots"];
    }
    $data = array();
    $data["p_type"] = ($type == "app" ? ($json["type"] == "game" ? "game" : "dlc") : "bundle");
    $data["author_id"] = (sizeof($json["developers"]) > 0 ? implode(", ", $json["developers"]) : "NULL");
    $data["name"] = $json["name"];
    $data["parents_id"] = ($json["type"] == "dlc" ? $json["fullgame"]["appid"] : "NULL");
    $data["date_r"] = ($json["release_date"]["coming_soon"] == "true" ? "NRY" : ($json["release_date"]["date"] != "" ? strtotime($json["release_date"]["date"]) : "NULL"));
    $data["composers"] = (sizeof($json["publishers"]) > 0 ? implode(", ", $json["publishers"]) : "NULL");
    $data["description"] = ($type == "app" ? $json["detailed_description"] : "NULL");
    $data["inv_description"] = ($type == "app" ? $json["about_the_game"] : "NULL");
    $data["price"] = (sizeof($json["price_overview"]) > 0 ? preg_replace("/([0-9]{2})$/", ".$1", $json["price_overview"]["initial"]) : preg_replace("/([0-9]{2})$/", ".$1", $json["price"]["initial"]));
    $data["price_n"] = $data["price"];
    for($i = 0; $i < sizeof($json["genres"]); $i++){
      $gen .= ($json["genres"][$i]["description"] != "Free to Play" ? ($gen == null ? "" : ",").$json["genres"][$i]["id"] : "");
    }
    $data["categories"] = $gen;
    $data["website"] = ($json["website"] != null ? rtrim($json["website"], "/") : "NULL");
    for($i = 0; $i < sizeof($json["categories"]); $i++){
      $fea .= ($i == 0 ? "" : ",").$json["categories"][$i]["id"];
    }
    $data["features"] = $fea;
    $data["platforms"] = ($json["platforms"]["windows"] ? "w*" : "").($json["platforms"]["mac"] ? "m*" : "").($json["platforms"]["linux"] ? "l*" : "");
    $data["meta"] = ($type == "app" ? $json["metacritic"]["score"] : "NULL");
    $data["meta_url"] = ($type == "app" ? $json["metacritic"]["url"] : "NULL");
    if(sizeof($json["achievements"]["highlighted"]) > 0){
      for($i = 0; $i < sizeof($json["achievements"]["highlighted"]); $i++){
        $ach .= ($i == 0 ? "" : "\\\\").$json["achievements"]["highlighted"][$i]["name"]."=".$json["achievements"]["highlighted"][$i]["path"];
      }
    }
    $data["achievements"] = (sizeof($json["achievements"]["highlighted"]) > 0 ? $json["achievements"]["total"]."\\\\\\\\".$ach : "NULL");
    $data["lang"] = ($json["supported_languages"] != null ? str_replace("<br>", '<br><span style="font-style:italic">', $json["supported_languages"])."</span>" : "NULL");
    $data["sid"] = $id;
    if($type == "sub"){
      $ids_a = explode(",", $ids);
      for($i = 0; $i < sizeof($ids_a); $i++){
        addSteamProduct($ids_a[$i]);
      }
    }
    if($type == "app" && sizeof($json["dlc"]) > 0){
      for($i = 0; $i < sizeof($json["dlc"]); $i++){
        addSteamProduct($json["dlc"][$i]);
      }
    }
    $data["childs_id"] = ($type == "app" ? (sizeof($json["dlc"]) > 0 ? implode(",", $json["dlc"]) : "NULL") : $ids);
    foreach($data as &$value) {
      $value = str_replace("'", "\'", $value);
      $value = ($value != "NULL" ? "'".$value."'" : $value);
    }
    if(!$result = mysqli_query($connection, "INSERT INTO products (`".implode("`, `", array_keys($data))."`) VALUES (".implode(", ", array_values($data)).");"))
      return "001";
    $id2 = mysqli_insert_id($connection);
    if($type == "sub"){
      $result = mysqli_query($connection, "SELECT SUM(price) AS price_i FROM products WHERE sid IN(".$ids.") AND (p_type = 'game' OR p_type = 'dlc');");
      $data2 = mysqli_fetch_array($result, MYSQLI_ASSOC);
      $result = mysqli_query($connection, "UPDATE products SET price_i = '".$data2["price_i"]."' WHERE id = '".$id2."' AND p_type = 'bundle';");
      $result = mysqli_query($connection, "SELECT GROUP_CONCAT(name SEPARATOR ', ') AS names FROM products WHERE sid IN(".$ids.") AND (p_type = 'game' OR p_type = 'dlc');");
      $data2 = mysqli_fetch_array($result, MYSQLI_ASSOC);
      $result = mysqli_query($connection, "UPDATE products SET inv_description = 'A Steam bundle including the following games: ".$data2["names"]."' WHERE id = '".$id2."' AND p_type = 'bundle';");
    }
    if(getimagesize($json["header_image"]) === false){
      resizeImage($json["page_image"], 0, 136, "content/images/products/".$id2.".png", true);
      cutImage("http://www.xriuk.com/content/images/products/".$id2.".png", 292, 136, "content/images/products/".$id2.".png");
    }
    else
      resizeImage($json["header_image"], 292, 136, "content/images/products/".$id2.".png", true);
    $rand = rand(0, sizeof($json["screenshots"]) - 1);
    createBackground($json["screenshots"][$rand]["path_full"], "content/images/products_backgrounds/".$id2.".png");
    return $id2;
  }
}

基本上我运行这个addSteamProduct(32848, "sub");,脚本从http://store.steampowered.com/api/packagedetails/?cc=it&l=english&packageids=32848获取JSON,然后执行所有操作。包的JSON(32848)有一系列包含的应用程序,其中一些应用程序可能有一个dlc数组,我需要解析所有这些,所以我遵循这条路径:

- Get package info + apps list
- Check if every app already exists in DB, if it does, quit
- Get app info + DLCs list
- Get every DLC info and add to DB if not already exists
- Add app info to DB
- Repeat for every app
- Add package info to DB

这似乎没有用,我已经删除了所有的PHP和MySQL时间限制,但是页面停止加载并且请求被解析了一半,当我删除创建的MySQL行再次尝试时我注意到一些奇怪的东西,我只有5行我需要保持不变,所以我删除 id&gt;的每一行5 ,运行此查询后,我注意到MySQL添加了PHP之前请求的左行 我的问题是所有这些代码适用于少于10个没有dlc的应用程序的软件包,但是这个代码有9个应用程序,其中一个有45个dlc,所以它停止工作。

对不起我的英语,如果你不明白我写的或需要更多信息,请在downvoting之前告诉我。
谢谢。

修改
我检查代码,然后页面加载,然后停止,MySQL查询一直运行直到完成。我不知道为什么页面加载会中断......

0 个答案:

没有答案