dongzhuo1733 2017-06-26 13:00
浏览 109
已采纳

PHP在一个页面上显示2个表

I am using this syntax to display a PHP Table on my page. I now need to add in a second table directly above this one, but all the syntax I try throws a 500 error. How can I with 1 connection to MSSQL run 2 Select statements and populate 2 individual html tables?

    $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>";
    }
}

EDIT
This is the syntax I am trying to adapt, but I keep getting a 500 Error

    $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>";
    }
}
  • 写回答

2条回答 默认 最新

  • dongyied24121 2017-06-26 14:38
    关注

    The problem you're having is that you are using the calls incorrectly.

    $query = $db->getQuery(true);
    $query = "SELECT name, MAX(Pay) As PayYTD FROM HRINFO";
    $db->setQuery($query);
    

    The first line will create an object, it doesn't matter which. The object will be in $query.

    The second line will immediately destroy the object and assign a string to $query (this is incorrect).

    The third line expects an object as a parameter to setQuery, but unfortunately it is a string! Error.

    If you want this to work correctly, then you need to use the object in $query correctly.

    I'm not a Joomla expert, so I link you to a page for how to do this correctly: https://docs.joomla.org/Selecting_data_using_JDatabase

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line