2010-09-07 47 views
1

這用於工作正常,直到昨晚,它出現源文件更改,所以我改變了我的爆炸嘗試修復它,但我仍然得到錯誤。PHP注意:導入數據時未定義的偏移量

的源代碼定義告訴我的領域是:

#export_date^Aapplication_id^Alanguage_code^Atitle^Adescription^Arelease_notes^Acompany_url^Asupport_url^Ascreenshot_url_1^Ascreenshot_url_2^Ascreenshot_url_3^Ascreenshot_url_4^Ascreenshot_width_height_1^Ascreenshot_width_height_2^Ascreenshot_width_height_3^Ascreenshot_width_height_4^Aipad_screenshot_url_1^Aipad_screenshot_url_2^Aipad_screenshot_url_3^Aipad_screenshot_url_4^Aipad_screenshot_width_height_1^Aipad_screenshot_width_height_2^Aipad_screenshot_width_height_3^Aipad_screenshot_width_height_4^B 

#dbTypes:BIGINT^AINTEGER^AVARCHAR(20)^AVARCHAR(1000)^ALONGTEXT^ALONGTEXT^AVARCHAR(1000)^AVARCHAR(1000)^AVARCHAR(1000)^AVARCHAR(1000)^AVARCHAR(1000)^AVARCHAR(1000)^AVARCHAR(20)^AVARCHAR(20)^AVARCHAR(20)^AVARCHAR(20)^AVARCHAR(1000)^AVARCHAR(1000)^AVARCHAR(1000)^AVARCHAR(1000)^AVARCHAR(20)^AVARCHAR(20)^AVARCHAR(20)^AVARCHAR(20)^B 

我的代碼是

$eoldelimiter = chr(2) . "\n"; 
$delimiter = chr(1); 
    while (!feof($fp3)) { 
     $line = stream_get_line($fp3,8000,$eoldelimiter); 
     if ($line[0] === '#') continue; //Skip lines that start with # 
     list($export_date, $application_id, $language_code, $title, $description, $release_notes, $company_url, $suppport_url, $screenshot_url_1, $screenshot_url_2, $screenshot_url_3, $screenshot_url_4, $screenshot_width_height_1, $screenshot_width_height_2, $screenshot_width_height_3, $screenshot_width_height_4,$ipadscreenshot_url_1, $ipadscreenshot_url_2, $ipadscreenshot_url_3, $ipadscreenshot_url_4, $ipadscreenshot_width_height_1, $ipadscreenshot_width_height_2, $ipadscreenshot_width_height_3, $ipadscreenshot_width_height_4) = explode($delimiter, $line); 
    } // end while statement 

,我在屏幕上得到的錯誤是

PHP Notice: Undefined offset: 23 in /var/www/vhosts/httpdocs/fred/daily_iapps_to_mysql.php on line 73

Notice: Undefined offset: 23 in /var/www/vhosts/httpdocs/fred/daily_iapps_to_mysql.php on line 73 PHP Notice: Undefined offset: 22 in /var/www/vhosts/httpdocs/fred/daily_iapps_to_mysql.php on line 73

Notice: Undefined offset: 22 in /var/www/vhosts/httpdocs/fred/daily_iapps_to_mysql.php on line 73 PHP Notice: Undefined offset: 21 in /var/www/vhosts/httpdocs/fred/daily_iapps_to_mysql.php on line 73

Notice: Undefined offset: 21 in /var/www/vhosts/httpdocs/fred/daily_iapps_to_mysql.php on line 73 PHP Notice: Undefined offset: 20 in /var/www/vhosts/httpdocs/fred/daily_iapps_to_mysql.php on line 73

Notice: Undefined offset: 20 in /var/www/vhostshttpdocs/fred/daily_iapps_to_mysql.php on line 73 PHP Notice: Undefined offset: 19 in /var/www/vhosts/httpdocs/fred/daily_iapps_to_mysql.php on line 73

+0

我建議你刪除您的域名從日誌 - 你的錯誤智能.... COM – 2010-09-07 10:01:08

+0

我已經做了所以想知道你爲什麼這麼建議? – kitenski 2010-09-07 12:10:32

回答

2

除了M42's answer:我建議使用像

$eoldelimiter = chr(2) . "\n"; 
$delimiter = chr(1); 
$cols = array(// you can even derive this from the comment line ...if you want to. 
    'export_date', 'application_id', 'language_code', 'title', 
    'description', 'release_notes', 'company_url', 'suppport_url', 
    'screenshot_url_1', 'screenshot_url_2', 'screenshot_url_3', 'screenshot_url_4', 
    'screenshot_width_height_1', 'screenshot_width_height_2', 'screenshot_width_height_3', 'screenshot_width_height_4', 
    'ipadscreenshot_url_1', 'ipadscreenshot_url_2', 'ipadscreenshot_url_3', 'ipadscreenshot_url_4', 
    'ipadscreenshot_width_height_1', 'ipadscreenshot_width_height_2', 'ipadscreenshot_width_height_3', 'ipadscreenshot_width_height_4' 
); 

$fp3 = fopen('test.txt', 'rb'); 
$data = array(); 
while(!feof($fp3)) { 
    $line = stream_get_line($fp3, 8000, $eoldelimiter); 
    if ('#'===$line[0]) { 
    continue; 
    } 

    $row = explode($delimiter, $line); 
    if (count($row) != count($cols)) { 
    echo 'wrong number of fields: (', count($row), ') ', $line, "\n"; 
    } 
    else { 
    $row = array_combine($cols, $row); 
    } 
    $data[] = $row; 
} 

,而不是使用24級獨立的變量和列表()


更新的東西:你可能有興趣在MySQL的LOAD DATA INFILE和/或準備,參數化的陳述,例如通過PHP Data Objects (pdo)
但無論如何,這裏是一個例子(例如,不生產代碼)爲建設一個查詢字符串...

$eoldelimiter = chr(2) . "\n"; 
$delimiter = chr(1); 
$cols = array(// you can even derive this from the comment line ...if you want to. 
    'export_date', 'application_id', 'language_code', 'title', 
    'description', 'release_notes', 'company_url', 'suppport_url', 
    'screenshot_url_1', 'screenshot_url_2', 'screenshot_url_3', 'screenshot_url_4', 
    'screenshot_width_height_1', 'screenshot_width_height_2', 'screenshot_width_height_3', 'screenshot_width_height_4', 
    'ipadscreenshot_url_1', 'ipadscreenshot_url_2', 'ipadscreenshot_url_3', 'ipadscreenshot_url_4', 
    'ipadscreenshot_width_height_1', 'ipadscreenshot_width_height_2', 'ipadscreenshot_width_height_3', 'ipadscreenshot_width_height_4' 
); 
$mysql = mysql_connect(...) or trigger_error(mysql_error()); 
mysql_select_db('test', $mysql) or trigger_error(mysql_error($mysql)); 

$sql_pre= 'INSERT INTO foo (' . join($cols, ',') . ') VALUES ('; 
$fp3 = fopen('test.txt', 'rb') or trigger_error('fopen failed'); 
while(!feof($fp3)) { 
    $line = stream_get_line($fp3, 8000, $eoldelimiter); 
    if ('#'===$line[0]) { 
    continue; 
    } 

    $row = explode($delimiter, $line); 
    if (count($row) != count($cols)) { 
    echo 'wrong number of fields: (', count($row), ') ', $line, "\n"; 
    } 
    else { 
    // the & in &$col will only work with php5+ 
    foreach($row as &$col) { 
     $col = "'".mysql_real_escape_string($col, $mysql)."'"; 
    } 
    $sql = $sql_pre . join(',', $row) . ')'; 
    echo $sql, "\n"; 
    } 
} 
+0

不幸的是,代碼給了我一個內存錯誤 - PHP致命錯誤:允許內存大小33554432字節耗盡(試圖分配641字節)在/var/www/vhosts/httpdocs/fred/test.php在80行是這一行$ line = stream_get_line($ fp3,8000,$ eoldelimiter); – kitenski 2010-09-07 12:56:03

+0

在$ data中存儲所有行只是一個例子......顯然,在你的情況下不可行;-)只要用你想做的一行就可以替換$ data [] = $ row;'。 – VolkerK 2010-09-07 13:29:08

+0

抱歉,但我對此相當陌生,不太理解答案。爆炸後,我做了一個MySQL插入,所以我需要在變量中的每個字段,所以我可以執行插入語句。我真的很抱歉,但是我不明白我是如何從示例代碼中做到這一點的。 – kitenski 2010-09-07 13:56:48

0

看來explode($delimiter, $line)沒有按沒有給出適當數量的元素。

你應該print_rexplode的結果來看看是否屬於這種情況。