【问题标题】:PHP Notice: Undefined offset when importing dataPHP 注意:导入数据时未定义偏移量
【发布时间】:2010-09-07 09:56:50
【问题描述】:

这在昨晚之前一直运行良好,但似乎源文件已更改,因此我更改了爆炸以尝试修复它,但仍然出现错误。

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

#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 in /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 行

【问题讨论】:

  • 我建议您从错误日志中删除您的域-smart....com
  • 我已经这样做了,但想知道您为什么建议这样做?

标签: php offset


【解决方案1】:

除了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";
  }
}

【讨论】:

  • 不幸的是,该代码给了我一个内存错误 - PHP 致命错误:/var/www/vhosts/httpdocs/fred/test.php 中允许的内存大小为 33554432 字节用尽(试图分配 641 字节)在第 80 行,即这一行 $line = stream_get_line($fp3, 8000, $eoldelimiter);
  • 将所有行存储在 $data 中只是一个示例……显然,在您的情况下这是不可行的;-) 只需将 $data[]=$row; 替换为您想要对单行执行的任何操作。
  • 对不起,但我对此很陌生,不太明白答案。爆炸后我做了一个 MySQL 插入,所以我需要一个变量中的每个字段,这样我就可以执行一个插入语句。真的很抱歉,但我无法从您的示例代码中看到我是如何做到的。
  • 您是否使用诸如“INSERT ... VALUES ('a', 'b', 'c')”之类的查询(即参数/数据混合到实际的 sql 语句中,例如通过 mysql_query( )) 还是使用准备好的参数化语句(例如通过 $pdo->prepare())?
  • 我在您的示例中使用第一种类型,INSERT ... VALUES ('a', 'b', 'c')"
【解决方案2】:

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

你应该print_rexplode的结果,看看是不是这样。

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 2015-08-01
    相关资源
    最近更新 更多