PHP注意:导入数据时未定义的偏移量

时间:2010-09-07 09:56:51

标签: php offset

以前一直工作到昨晚,看来源文件发生了变化,所以我改变了我的爆炸试图修复它,但我仍然得到错误。

源代码定义告诉我字段是:

#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注意:未定义的偏移:23英寸   /var/www/vhosts/httpdocs/fred/daily_iapps_to_mysql.php   第73行

     

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

     

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

     

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

     

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

2 个答案:

答案 0 :(得分: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个独立变量和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";
  }
}

答案 1 :(得分:0)

似乎explode($delimiter, $line)没有给出正确数量的元素。

您应该print_r explode的结果来查看是否属实。