我得到了一個函數,它從我的數據庫中請求一些信息,將它放入一個數組並將其返回。 sql語句是正確的。未定義的變量:行
當在數據庫中找不到當前日期時,我想在我的網站上顯示一個錯誤。 錯誤消息,也被添加到返回數組中。
首先我用COUNT檢查數據庫中是否存在相應的日期,如果count == 1,我用另一個語句從數據庫中獲取所有數據。如果count!= 1,我將一個數組硬編碼放在一起。
當日期是在相同的給定參數的數據庫中找到,該腳本用得好好的,但是當我更改日期在數據庫中,我得到以下錯誤:
1252年至1257年Notice: Undefined variable: row in /Applications/MAMP/htdocs/models/funcs.php on line 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)
如果沒有行呢?既然你沒有在你的循環之前定義'$ rows',它永遠不會存在(並且是一個* undefined *變量!) – h2ooooooo 2015-02-09 18:27:18