MySQL服务器已经消失

时间:2015-07-21 11:33:32

标签: php mysql web-crawler

我尝试将数据存储到数据库中。但我得到“MySQL服务器消失”的错误。任何人都可以帮我找出我的错误吗?

实际上这是Basic PHP爬虫之一。我是这个人的初学者。所以帮帮我!!!

Connection.php:

<?php
$hostname_TestConnection = "******";
$database_TestConnection = "*******";
$username_TestConnection = "*******";
$password_TestConnection = "********";
$TestConnection = mysqli_connect($hostname_TestConnection, $username_TestConnection, $password_TestConnection) or trigger_error(mysqli_error($TestConnection),E_USER_ERROR); 
?>

的index.php:

<?php require_once('Connections/TestConnection.php');
include_once('simple_html_dom.php');  

ini_set('mysql.connect_timeout', -1);
ini_set('default_socket_timeout', -1);

if (!function_exists("GetSQLValueString")) {
 function GetSQLValueString($theValue, $theType,  $TestConnection, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ?  mysqli_real_escape_string($TestConnection, $theValue) :  mysqli_escape_string($TestConnection, $theValue);

switch ($theType) {
 case "text":
   $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
   break;    
 case "long":
 case "int":
   $theValue = ($theValue != "") ? intval($theValue) : "NULL";
   break;
 case "double":
   $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
   break;
 case "date":
   $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
   break;
 case "defined":
   $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
   break;
 }
 return $theValue;
 }
}

$target_url = "***";
$html = new simple_html_dom();
$html_sub_page = new simple_html_dom();
$html->load_file($target_url);

//Declare elements {......}

//Retrieve latest ID from table {....}

//Main Page content {....}

//Sub Page content {....}

//Insert Into DB

if($flag == 1)
{
    for($i = 0 ; $i < $counter ; $i++)
    {

    $latestid ++;
    $guid .= $latestid;
    $post_name = $latestid.$post_name;

    $insertSQL = sprintf("INSERT INTO test_posts (post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_content_filtered, post_parent, guid, menu_order, post_type,post_mine_type, comment_count) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($post_author, "int", $TestConnection),
                       GetSQLValueString($post_date, "date", $TestConnection),
                       GetSQLValueString($post_date_gmt, "date", $TestConnection),
                       GetSQLValueString($post_title_array[$i], "text", $TestConnection),
                       GetSQLValueString($post_title_array[$i], "text", $TestConnection),
                       GetSQLValueString($post_excerpt, "text", $TestConnection),
                       GetSQLValueString($post_status, "text", $TestConnection),
                       GetSQLValueString($comment_status, "text", $TestConnection),
                       GetSQLValueString($ping_status, "text", $TestConnection),
                       GetSQLValueString($post_password, "text", $TestConnection),
                       GetSQLValueString($post_name, "text", $TestConnection),
                       GetSQLValueString($to_ping, "text", $TestConnection),
                       GetSQLValueString($pinged, "text", $TestConnection),
                       GetSQLValueString($post_modified, "date", $TestConnection),
                       GetSQLValueString($post_modified_gmt, "date", $TestConnection),
                       GetSQLValueString($post_content_filtered, "text", $TestConnection),
                       GetSQLValueString($post_parent, "int", $TestConnection),
                       GetSQLValueString($guid, "text", $TestConnection),
                       GetSQLValueString($menu_order, "int", $TestConnection),
                       GetSQLValueString($post_type, "text", $TestConnection),
                       GetSQLValueString($post_mine_type, "text", $TestConnection),
                       GetSQLValueString($comment_count, "int", $TestConnection));

  mysqli_select_db($TestConnection,$database_TestConnection);
  $Result1 = mysqli_query($TestConnection, $insertSQL) or die(mysqli_error($TestConnection));

  $post_name = "-revision-v1"; 
  $guid = '****';
  echo "<br>Data ". ($i+1) . "succssfully inserted Into DataBase";
  //sleep(3);
 }
}

mysqli_free_result($RetrieveLatestID);
mysqli_close($TestConnection);
?>

1 个答案:

答案 0 :(得分:1)

来自Link

MySQL服务器已经消失(错误2006)有两个主要原因和解决方案:

  • 服务器超时并关闭连接。要修复,请检查my.cnf配置文件中的“wait_timeout”mysql变量是否足够大。

  • 服务器丢弃了错误或过大的数据包。如果mysqld获取的数据包太大或不正确,则会假定客户端出现问题并关闭连接。要修复,您可以增加my.cnf文件中的最大数据包大小限制“max_allowed_pa​​cket”,例如。设置max_allowed_pa​​cket = 128M,然后sudo /etc/init.d/mysql restart。

您有关于问题的更完整信息Here