【发布时间】:2017-11-29 08:00:48
【问题描述】:
我正在使用这种语法在我的页面上显示一个 PHP 表。我现在需要在该表正上方添加第二个表,但我尝试的所有语法都会引发 500 错误。如何通过 1 个与 MSSQL 的连接运行 2 个 Select 语句并填充 2 个单独的 html 表?
$option = array();
$option['driver'] = 'mssql';
$option['host'] = 'IP Address';
$option['user'] = 'UserName';
$option['password'] = 'Password';
$option['database'] = 'DB';
$option['prefix'] = '';
$db = JDatabase::getInstance($option);
$query = $db->getQuery(true);
$query = "SELECT name, hiredate, bday, payrate, hourlypay from HRData ORDER BY name ASC";
$db->setQuery($query);
$query = $db->loadObjectList();
if ($query)
{
?>
<table border="1">
<thead>
<tr>
<th>Name </th>
<th>Hire Date </th>
<th>Birthday </th>
<th>Pay Rate </th>
<th>hourlypay </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tr>";
print "<td>" . $res->name . "</td>";
print "<td>" . $res->hiredate . "</td>";
print "<td>" . $res->bday . "</td>";
print "<td>" . $res->payrate . "</td>";
print "<td>" . $res->hourlypay . "</td>";
print "</tr>";
}
}
编辑
这是我正在尝试调整的语法,但我不断收到 500 错误
$option = array();
$option['driver'] = 'mssql';
$option['host'] = 'IP Address';
$option['user'] = 'UserName';
$option['password'] = 'Password';
$option['database'] = 'DB';
$option['prefix'] = '';
$db = JDatabase::getInstance($option);
$query = $db->getQuery(true);
$query = "SELECT name, MAX(Pay) As PayYTD FROM HRINFO";
$db->setQuery($query);
$query = $db->loadObjectList();
if ($query)
{
?>
<table border="1">
<thead>
<tr>
<th>Name </th>
<th>YTD Pay </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tr>";
print "<td>" . $res->name . "</td>";
print "<td>" . "$" . round($res->PayYTD) . "</td>";
print "</tr>";
}
}
<br><br><br>
//Query
$query = $db->getQuery(true);
$query = "SELECT name, hiredate, bday, payrate, hourlypay from HRData ORDER BY name ASC";
$db->setQuery($query);
$query = $db->loadObjectList();
if ($query)
{
?>
<table border="1">
<thead>
<tr>
<th>Name </th>
<th>Hire Date </th>
<th>Birthday </th>
<th>Pay Rate </th>
<th>hourlypay </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tr>";
print "<td>" . $res->name . "</td>";
print "<td>" . $res->hiredate . "</td>";
print "<td>" . $res->bday . "</td>";
print "<td>" . $res->payrate . "</td>";
print "<td>" . $res->hourlypay . "</td>";
print "</tr>";
}
}
【问题讨论】:
-
在此之后像往常一样运行下一个查询而不关闭连接。它应该可以工作。
-
我是否使用相同的变量 $query?就像这样 $db = JDatabase::getInstance($option); $query = $db->getQuery(true);
-
是的,您可以使用相同的变量。更改查询,和往常一样,您需要编写这些语句
$db->setQuery($query);$query = $db->loadObjectList(); -
您似乎对 setQuery 和 getQuery 直接运行 SQL 语句的作用感到困惑。你应该遵循这里的语法:docs.joomla.org/Selecting_data_using_JDatabase
-
@BellHopByDayAmetuerCoderByNigh 您不能只在 php 文件中正确处理纯 html。您要么必须回显它,要么像
echo "<br><br><br>";一样打印它。您要么没有正确粘贴源代码,要么粘贴后正在编辑源代码。
标签: php html joomla html-table joomla3.0