2015-02-09 176 views
0

我得到了一個函數,它從我的數據庫中請求一些信息,將它放入一個數組並將其返回。 sql語句是正確的。未定義的變量:行

當在數據庫中找不到當前日期時,我想在我的網站上顯示一個錯誤。 錯誤消息,也被添加到返回數組中。

首先我用COUNT檢查數據庫中是否存在相應的日期,如果count == 1,我用另一個語句從數據庫中獲取所有數據。如果count!= 1,我將一個數組硬編碼放在一起。

當日期是在相同的給定參數的數據庫中找到,該腳本用得好好的,但是當我更改日期在數據庫中,我得到以下錯誤:

Notice: Undefined variable: row in /Applications/MAMP/htdocs/models/funcs.php on line 1257

1252年至1257年

線:

while($stmt->fetch()) 
      { 
       $row[] = array('id' => $id, 'datum' => $datum, 'mac' => $mac, 'ipad' => $ipad, 'iphone' => $iphone, 'imember' => $imember, 'applecare' => $applecare, 'verkoop_ochtend' => $verkoop_ochtend, 'verkoop_middag' => $verkoop_middag, 'verkoop_avond' => $verkoop_avond, 'vracht_ochtend' => $vracht_ochtend, 'vracht_middag' => $vracht_middag, 'vracht_avond' => $vracht_avond, 'service_ochtend' => $service_ochtend, 'service_middag' => $service_middag, 'service_avond' => $service_avond, 'werkzaamheden' => $werkzaamheden, 'error' => ''); 
      } 
      $stmt->close(); 
      $data = $row; 

有誰知道我在做什麼錯在這裏?提前致謝!

完整的功能:

function get_current_planning($date) 
{ 
    GLOBAL $mysqli, $db_table_prefix; 
    $stmt = $mysqli->prepare("SELECT COUNT(datum) FROM " . $db_table_prefix . "planning WHERE datum = '" . $date . "'"); 
    $result = $stmt->execute(); 
print_r($result); 
var_dump($result); 

    if($result == 1) 
    { 
     $stmt->prepare("SELECT 
             * 
             FROM " . $db_table_prefix . "planning 
             WHERE datum = '" . $date . "'"); 
     $stmt->execute(); 
     $stmt->bind_result($id, $datum, $mac, $ipad, $iphone, $imember, $applecare, $verkoop_ochtend, $verkoop_middag, $verkoop_avond, $vracht_ochtend, $vracht_middag, $vracht_avond, $service_ochtend, $service_middag, $service_avond, $werkzaamheden); 
     while($stmt->fetch()) 
      { 
       $row[] = array('id' => $id, 'datum' => $datum, 'mac' => $mac, 'ipad' => $ipad, 'iphone' => $iphone, 'imember' => $imember, 'applecare' => $applecare, 'verkoop_ochtend' => $verkoop_ochtend, 'verkoop_middag' => $verkoop_middag, 'verkoop_avond' => $verkoop_avond, 'vracht_ochtend' => $vracht_ochtend, 'vracht_middag' => $vracht_middag, 'vracht_avond' => $vracht_avond, 'service_ochtend' => $service_ochtend, 'service_middag' => $service_middag, 'service_avond' => $service_avond, 'werkzaamheden' => $werkzaamheden, 'error' => ''); 
      } 
      $stmt->close(); 
      $data = $row; 
    } 
    else 
    { 
     $row[] = array('id' => '', 'datum' => '', 'mac' => '', 'ipad' => '', 'iphone' => '', 'imember' => '', 'applecare' => '', 'verkoop_ochtend' => '', 'verkoop_middag' => '', 'verkoop_avond' => '', 'vracht_ochtend' => '', 'vracht_middag' => '', 'vracht_avond' => '', 'service_ochtend' => '', 'service_middag' => '', 'service_avond' => '', 'werkzaamheden' => '', 'error' => 'Er is geen planning gevonden voor de huidige datum!'); 
     $stmt->close(); 
     $data = $row; 
    } 
    return $data; 
} 

print_r($result) returns 1

var_dump($result) returns bool(true)

+0

如果沒有行呢?既然你沒有在你的循環之前定義'$ rows',它永遠不會存在(並且是一個* undefined *變量!) – h2ooooooo 2015-02-09 18:27:18

回答

1

您不是您的SELECT COUNT()...查詢的結果。它在我看來像你的第二個查詢中的while(...fetch())循環有時會運行零次。這將導致代碼開始$row[] =無法運行,這反過來會導致$rowwhile(...fetch())循環之後未定義。

無論如何,用兩個連續的查詢來計算行數然後獲取它們有點浪費。您可能希望完全跳過SELECT COUNT查詢。相反,做這樣的事情:

$row = Array(); 
while($stmt->fetch()) { 
    $row[] = array('id' => $id, 'datum' => $datum, 'mac' => $mac, 'ipad' => $ipad, 'iphone' => $iphone, 'imember' => $imember, 'applecare' => $applecare, 'verkoop_ochtend' => $verkoop_ochtend, 'verkoop_middag' => $verkoop_middag, 'verkoop_avond' => $verkoop_avond, 'vracht_ochtend' => $vracht_ochtend, 'vracht_middag' => $vracht_middag, 'vracht_avond' => $vracht_avond, 'service_ochtend' => $service_ochtend, 'service_middag' => $service_middag, 'service_avond' => $service_avond, 'werkzaamheden' => $werkzaamheden, 'error' => ''); 
} 
$stmt->close(); 
if (0 == count($row)) { 
    /* deal with the no matching rows case */ 
} 
$data = $row; 
-1

看起來你還沒有定義的變量$row。在開始將$row作爲數組對待之前,您需要一個$row=Array();

+0

這並不完全正確。你可以編寫'$ row [] ='X''來給數組添加一個值,如果數組還沒有被定義,數組將被定義。但是,如果查詢不返回任何行,那麼'while'循環沒有迭代,'$ row'不會被定義。在這種情況下'$ data = $ row;'將會失敗。你的解決方案將解決這個問題,爲了清晰和像這樣的意外行爲,初始化像這樣的變量通常是一個好主意。 – GolezTrol 2015-02-09 18:13:08

+0

@GolezTrol是的,它將被定義,但PHP會「注意到」你這件事。總是建議聲明所有變量。推薦閱讀:http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index – nikoskip 2015-02-09 18:18:10

+0

@nikoskip我不認爲這是速記數組符號的情況。這是通過'$ data = $ row;'(= 1257行)行的通知來支持的。另請參閱[關於數組的PHP文檔](http://php.net/manual/en/language.types.array.php)上的「使用方括號語法創建/修改」一章。 (「創建」有點放棄它)。不過,我完全同意你初始化變量。 – GolezTrol 2015-02-09 18:40:48