我在我的语法if (query)
中有这一行,我认为它会检查查询是否为null,但是我的查询返回0行并且仍然显示标题行。
在显示表之前,确保查询返回行的正确方法是什么?
<?php
$option = array();
$option['driver'] = 'mssql';
$option['host'] = 'ServerName';
$option['user'] = 'username';
$option['password'] = 'password';
$option['database'] = 'database';
$option['prefix'] = '';
$db = JDatabase::getInstance($option);
$query = $db->getQuery(true);
$query = "SELECT blue, green, red, purple, pink from colors";
$db->setQuery($query);
$query = $db->loadObjectList();
if (query)
{
?>
<div id="dvData">
<table id="example" border="1">
<caption><strong style="font-size:25px;"><?php echo $startdate ?> - <?php echo $enddate ?></strong></caption>
<thead>
<tr>
<th bgcolor="#00FF00">Blue </th>
<th bgcolor="#00FF00">Green </th>
<th bgcolor="#00FF00">Red </th>
<th bgcolor="#00FF00">Purple </th>
<th bgcolor="#00FF00">Pink </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tr>";
print "<td>" . $res->Blue . "</td>";
print "<td>" . $res->Green . "</td>";
print "<td>" . $res->Red . "</td>";
print "<td>" . $res->Purple . "</td>";
print "<td>" . $res->Pink . "</td>";
print "</tr>";
}
}
?>
</table>
</div>