我正在使用此语法在我的页面上显示PHP表。我现在需要在这一个的正上方添加第二个表,但我尝试的所有语法都会抛出500错误。如何通过1连接到MSSQL运行2选择语句并填充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>";
}
}
答案 0 :(得分:3)
您遇到的问题是您使用的电话不正确。
$query = $db->getQuery(true);
$query = "SELECT name, MAX(Pay) As PayYTD FROM HRINFO";
$db->setQuery($query);
第一行会创建一个对象,哪个无关紧要。该对象将在$ query中。
第二行将立即销毁对象并为$ query分配一个字符串(这是不正确的)。
第三行需要一个对象作为setQuery的参数,但不幸的是它是一个字符串!错误。
如果您希望它正常工作,那么您需要正确使用$ query中的对象。
我不是Joomla专家,因此我将您链接到有关如何正确执行此操作的页面:https://docs.joomla.org/Selecting_data_using_JDatabase
答案 1 :(得分:-1)
将输出放在变量中。
$StringOut = '';
$StringOut .= '<table border="1">
<thead>
<tr>
<th>Name </th>
<th>Hire Date </th>
<th>Birthday </th>
<th>Pay Rate </th>
<th>hourlypay </th>
</tr>
</thead>';
foreach ($query as $res)
{
$StringOut .= "<tr>";
$StringOut .= "<td>" . $res->name . "</td>";
$StringOut .= "<td>" . $res->hiredate . "</td>";
$StringOut .= "<td>" . $res->bday . "</td>";
$StringOut .= "<td>" . $res->payrate . "</td>";
$StringOut .= "<td>" . $res->hourlypay . "</td>";
$StringOut .= "</tr>";
}
$StringOut .= '</table>';
#Do other logic to retrieve first table.
#You could put it in a different variable if you like. And print when and whereever you wish.
echo $StringOut;
#Optionally close connection, done.