我有一个函数,它从我的数据库中请求一些信息,将它放在一个数组中并返回它。 sql语句是正确的。
如果在数据库中找不到当前日期,我想在我的网站上显示错误。 错误消息也会添加到返回数组中。
首先我用COUNT检查数据库中是否存在相应的日期,如果count == 1,我用另一个语句从数据库中获取所有数据。如果计数!= 1,我将数组硬编码放在一起。
当在数据库中找到与给定参数相同的日期时,脚本就像一个魅力,但当我更改数据库中的日期时,我得到以下错误:
注意:未定义的变量:行 第1257行/Applications/MAMP/htdocs/models/funcs.php
第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)返回1
var_dump($ result)返回bool(true)
答案 0 :(得分:1)
您未fetch()
SELECT COUNT()...
查询的结果。在我看来,您的第二个查询中的while(...fetch())
循环有时会运行零次。这将导致代码开始$row[] =
无法运行,这反过来会导致在$row
循环后while(...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 :(得分:-1)
看起来您还没有定义变量$row
。在开始将$row=Array();
视为数组之前,您需要$row
。