1

これは昨夜まで問題なく動作していましたが、ソース ファイルが変更されたようです。そのため、爆発を変更して修正しようとしましたが、それでもエラーが発生します。

ソースコード定義は、フィールドが次のとおりであることを示しています。

#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 通知: 未定義のオフセット: /var/www/vhosts/httpdocs/fred/daily_iapps_to_mysql.php の 73 行目に 23

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

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

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

Notice: 未定義のオフセット: 73 行目の /var/www/vhostshttpdocs/fred/daily_iapps_to_mysql.php の 20 PHP Notice: 未定義のオフセット: 19 行目の /var/www/vhosts/httpdocs/fred/daily_iapps_to_mysql.php

4

2 に答える 2

2

M42の答えに加えて:次のようなものを使用することをお勧めします

$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 個の独立変数と list() を使用する代わりに


更新: 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";
  }
}
于 2010-09-07T10:27:31.387 に答える
0

explode($delimiter, $line)正しい数の要素を与えていないようです。

print_rそうであるかどうかを確認するには、 の結果を確認する必要がありexplodeます。

于 2010-09-07T10:06:27.183 に答える