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

注意:未定义的偏移量:第 73 行的 /var/www/vhosts/httpdocs/fred/daily_iapps_to_mysql.php 中的 23 PHP 注意:未定义的偏移量:第 73 行的 /var/www/vhosts/httpdocs/fred/daily_iapps_to_mysql.php 中的 22

注意:未定义的偏移量:第 73 行的 /var/www/vhosts/httpdocs/fred/daily_iapps_to_mysql.php 中的 22 PHP 注意:未定义的偏移量:第 73 行的 /var/www/vhosts/httpdocs/fred/daily_iapps_to_mysql.php 中的 21

注意:未定义的偏移量:第 73 行的 /var/www/vhosts/httpdocs/fred/daily_iapps_to_mysql.php 中的 21 PHP 注意:未定义的偏移量:第 73 行的 /var/www/vhosts/httpdocs/fred/daily_iapps_to_mysql.php 中的 20

注意:未定义的偏移量:第 73 行的 /var/www/vhostshttpdocs/fred/daily_iapps_to_mysql.php 中的 20 PHP 注意:未定义的偏移量:第 73 行的 /var/www/vhosts/httpdocs/fred/daily_iapps_to_mysql.php 中的 19

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 数据对象 (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 回答