如果没有匹配的行,那么 MySQL 将返回一个空集(这里我随机定义了字段,只是为了运行查询):
mysql> CREATE TABLE dienstplan (kw varchar(10), datum integer, schicht integer, perso_id integer);
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE codes (lcfruh varchar(2));
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE personal (perso_id integer, status varchar(5));
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> select count(schicht) as front_lcfruh,
-> kw,
-> datum
-> from dienstplan
-> left join codes on dienstplan.schicht = codes.lcfruh
-> left join personal on personal.perso_id = dienstplan.perso_id
-> where codes.lcfruh != ''
-> and personal.status = 'rezeption'
-> and dienstplan.kw = '$kw'
-> group by dienstplan.datum;
Empty set (0.00 sec)
您可以检查查询返回了多少行。在这种情况下,您将得到零。
所以你可以像这样修改你的代码(伪代码):
$exec = $db->execute($query);
if ($exec->error() !== false) {
// Handle errors. Possibly quit or raise an exception.
}
if (0 == $exec->count())
{
// No rows. We return a default tuple
$tuple = array(
'front_lcfruh' => 0,
'kw' => $kw,
'datum' => null
);
handleTuple($tuple);
} else {
while($tuple = $exec->fetch()) {
handleTuple($tuple);
}
}
handleTuple()
格式化或以其他方式操作返回行的函数在哪里。