我有这个代码-
$previous_date = date('Y-m-d',strtotime("-14 days"));
$conn = testdb_connect ();
$getfamilydate= get_family_date ($previous_date);
if (! $row)
{
echo "No testcase being executed in the selected duration";
} else { while($row = $getfamilydate->fetch(PDO::FETCH_ASSOC))
{
foreach($row as $key)
{
$twoWeekfamily[] = $key;
}
}
}
在这里,我试图检查查询是否在指定的持续时间(前一个日期)内返回任何结果,如果没有,它应该显示msg-elset返回数组。
我有像这样的持续时间
1天、2天、1周、2周。。等等,并且我在所有持续时间内都使用相同的代码。此逻辑在1天内运行良好,即,如果没有数据显示消息,但对于所有其他逻辑,如果查询没有返回数据,则抛出未定义变量:行通知。
这里的逻辑有什么问题?
执行类似-的操作
while($row = $getfamilydate->fetch(PDO::FETCH_ASSOC))
{
foreach($row as $key)
{
$twoWeekfamily[] = $key;
}
}
if(empty( $twoWeekfamily))
{
echo "No testcase being executed in the selected duration";
}
像下面的一样更改代码
if (! $getfamilydate){
echo "No testcase being executed in the selected duration";
}
else {
$count = $getfamilydate->rowCount();
if($count > 0){
while($row = $getfamilydate->fetch(PDO::FETCH_ASSOC))
{
var_dump($row);
}
}
else{
echo "No rows found";
}
}
在您的代码中,在将值设置为$row 之前,您要检查$row
if (!$getfamilydate->rowCount()){
echo "No testcase being executed in the selected duration";
}else { while($row = $getfamilydate->fetch(PDO::FETCH_ASSOC))
{
foreach($row as $key)
{
$twoWeekfamily[] = $key;
}
}}
在if
语句中使用$getfamilydate->rowCount()
而不是$row
。