2015-07-20 114 views
-1

我是抓取网页内容的初学者。我试图从另一个网站检索内容。所以写了履带的代码。成功抓取另一个网页上的内容。我试图存储所有从其他网站爬行的数据。MySql服务器已经走了 - PHP

我得到“MySql服务器已经消失”错误。任何人都可以帮助我找出我的错误?

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); 
?> 
+0

错误是什么? –

+0

var_dump($ insertSQL)就在你插入之前.. – MaggsWeb

回答

0

为什么你从mysqli_切换到mysql_的错误信息?坚持mysqli_。虽然,mysqli_error()需要db句柄作为第一个参数,所以你不能在你拥有的内容中使用它。相反,使用mysqli_connect_error(),因为这不需要数据库句柄。

为什么你检查mysql_real...函数存在,然后使用mysqli_real..函数..?

+0

我已经改变我的代码为“$ TestConnection = mysqli_connect($ hostname_TestConnection,$ username_TestConnection,$ password_TestConnection)或trigger_error(mysqli_connect_error(),E_USER_ERROR);” –

+0

现在我检查我的连接状态。连接建立成功。然后更改为“$ Result1 = mysqli_query($ TestConnection,$ insertSQL)或die(mysqli_error($ TestConnection));” (在index.php中)。现在我得到的错误是:MySql服务器走了。 –

+0

这就是因为yopu在你的测试失败的错误中使用了一个变量。你不能在$ testConnection =的'else'中使用$ testConnection – MaggsWeb

相关问题