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 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入