2012-01-08 62 views
0

當我嘗試處理與命令行文件:錯誤時,命令行程序

cat texte.txt|php index.php 

我得到這個奇怪的堆棧:

C:\wamp\www\Fraud Prevention>cat texte.txt|php index.php 
PHP Notice: Undefined offset: 1 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 92 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 1 in C:\wamp\www\Fraud Prevention\index.php on line 92 


Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

PHP Notice: Undefined offset: 2 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 94 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 2 in C:\wamp\www\Fraud Prevention\index.php on line 94 


Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

PHP Notice: Undefined offset: 3 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 96 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 3 in C:\wamp\www\Fraud Prevention\index.php on line 96 


Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

PHP Notice: Undefined offset: 4 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 98 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 4 in C:\wamp\www\Fraud Prevention\index.php on line 98 


Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

PHP Notice: Undefined offset: 5 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 100 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 5 in C:\wamp\www\Fraud Prevention\index.php on line 10 
0 

Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

PHP Notice: Undefined offset: 6 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 102 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 6 in C:\wamp\www\Fraud Prevention\index.php on line 10 
2 

Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

PHP Notice: Undefined offset: 7 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 104 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 7 in C:\wamp\www\Fraud Prevention\index.php on line 10 
4 

Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

PHP Notice: Undefined offset: 1 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 92 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 1 in C:\wamp\www\Fraud Prevention\index.php on line 92 


Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

PHP Notice: Undefined offset: 2 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 94 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 2 in C:\wamp\www\Fraud Prevention\index.php on line 94 


Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

PHP Notice: Undefined offset: 3 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 96 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 3 in C:\wamp\www\Fraud Prevention\index.php on line 96 


Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

PHP Notice: Undefined offset: 4 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 98 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 4 in C:\wamp\www\Fraud Prevention\index.php on line 98 


Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

PHP Notice: Undefined offset: 5 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 100 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 5 in C:\wamp\www\Fraud Prevention\index.php on line 10 
0 

Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

PHP Notice: Undefined offset: 6 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 102 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 6 in C:\wamp\www\Fraud Prevention\index.php on line 10 
2 

Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

PHP Notice: Undefined offset: 7 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 104 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 7 in C:\wamp\www\Fraud Prevention\index.php on line 10 
4 

Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

PHP Notice: Undefined offset: 1 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 92 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 1 in C:\wamp\www\Fraud Prevention\index.php on line 92 


Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

PHP Notice: Undefined offset: 2 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 94 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 2 in C:\wamp\www\Fraud Prevention\index.php on line 94 


Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

PHP Notice: Undefined offset: 3 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 96 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 3 in C:\wamp\www\Fraud Prevention\index.php on line 96 


Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

PHP Notice: Undefined offset: 4 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 98 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 4 in C:\wamp\www\Fraud Prevention\index.php on line 98 


Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

PHP Notice: Undefined offset: 5 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 100 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 5 in C:\wamp\www\Fraud Prevention\index.php on line 10 
0 

Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

PHP Notice: Undefined offset: 6 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 102 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 6 in C:\wamp\www\Fraud Prevention\index.php on line 10 
2 

Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

PHP Notice: Undefined offset: 7 in C:\wamp\www\Fraud Prevention\index.php on li 
ne 104 
PHP Stack trace: 
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

Notice: Undefined offset: 7 in C:\wamp\www\Fraud Prevention\index.php on line 10 
4 

Call Stack: 
    0.0005  330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0 

編輯:

這裏是我的代碼在92和104之間:

$handle = fopen ("php://stdin","r"); 

    $N = $title = fgets($handle, 1024); 


    while(!feof($handle)) 
    { 
     if (!$order = fgets($handle)) continue; 
     $all_orders[] = explode(',', $order); 

    } 

    foreach ($all_orders as &$order){ 

    $order["Order id"]=$order[0]; 
    unset($order[0]); 
    $order["Deal id"]=$order[1]; 
    unset($order[1]); 
    $order["Email address"]=$order[2]; 
    unset($order[2]); 
    $order["Street address"]=$order[3]; 
    unset($order[3]); 
    $order["City"]=$order[4]; 
    unset($order[4]); 
    $order["State"]=$order[5]; 
    unset($order[5]); 
    $order["Zip Code"]=$order[6]; 
    unset($order[6]); 
    $order["Credit Card"]=$order[7]; 
    unset($order[7]); 

    } 

print_r($all_orders); 
+1

index.php,92-104行是什麼?您正在訪問不存在的數組項。 – Maerlyn 2012-01-08 16:11:33

+0

我已經更新了我的代碼,從我的代碼的第92到第104行有一個片段,我不知道,但是它在小時前很好地工作,我沒有改變任何東西。 – Malloc 2012-01-08 16:17:25

+1

@Malek你從'fgets()'調用中錯過了'trim()' - 我猜你已經有幾個空的項目了[你在幾個小時前](http:// stackoverflow。 com/questions/8777059)...'var_dump($ all_orders)'確認;-) – DaveRandom 2012-01-08 16:24:25

回答

3

在你的foreach循環中,你正在訪問數組值而不檢查它們是否存在。

PHP會在每次訪問不存在的索引時給你一個通知 - 在你的情況下,一個堆棧跟蹤(我假設它是XDebug)。通知不是一個明顯的錯誤,你的腳本只是繼續,並得到另一個通知,另一個,等等。

您可以用isset($order[$index])檢查索引'存在。

此外,您似乎正在閱讀CSV數據。如何使用PHP的內置fgetcsv()已經處理了許多邊緣情況下你的代碼不?

順便說一句,你的foreach循環是不必要的,你可以在讀取記錄後在while循環中完成那個處理。

+1

我已經建議'fgetcsv()',但OP似乎有一個問題, fgets()'爲其他記錄返回空行。他在我之前給他的一些代碼中缺少一個'trim()',可能會再次給他留下空白的記錄 - [見這裏](http://stackoverflow.com/questions/8777059)。 – DaveRandom 2012-01-08 16:37:33