PHP / MSSQL:查询结果到表

时间:2019-02-10 17:15:37

标签: php sql-server

我正在尝试将MS SQL查询的结果返回到表中。 但是不知何故,我只得到带有标题的空白页面。 我不知道自己在做什么错或在哪里寻找答案。 有人可以用正确的方式指导我。

感谢您的帮助

<?php
    $serverName = "192.168.8.8";
    $connectionOptions = array(
        "Database" => "GeoDynamics",
        "Uid" => "User",
        "PWD" => "Password"
    );
    //Establishes the connection
    $dbh = sqlsrv_connect($serverName, $connectionOptions);
$sql = "SELECT * FROM Aanbest WHERE ReceiptLimitDate = '2018-12-03'  order by ImportDatum desc"; 
$getResults= sqlsrv_query($dbh, $sql);
?>
<style type="text/css">
.tftable {font-size:12px;color:#333333;width:100%;border-width: 1px;border-color: #729ea5;border-collapse: collapse;}
.tftable th {font-size:12px;background-color:#acc8cc;border-width: 1px;padding: 8px;border-style: solid;border-color: #729ea5;text-align:left;}
.tftable tr {background-color:#d4e3e5;}
.tftable td {font-size:12px;border-width: 1px;padding: 8px;border-style: solid;border-color: #729ea5;}
.tftable tr:hover {background-color:#ffffff;}
</style>
<table class="tftable" border="1">
<thead>
    <tr>
    <th>ID</th>
    <th>Aanbestedingsdatum</th>
    <th>Klasse</th>
    <th>Omschrijving</th>
    <th>Postcode</th>
    <th>Stad</th>
    <th>Bestuur</th>
    <th>LikedBy?</th>
    <th>Like</th>
    </tr>
</thead>
<tbody>

<?php
    foreach ($dbh->query($sql) as $rows){
    ?>
    <tr>
        <td><?php echo $rows['ID']?></td>
        <td><?php echo $rows['ReceiptLimitDate']?></td>
        <td><?php echo $rows['Classes1']?></td>
        <td><?php echo $rows['Title']?></td>
        <td><?php echo $rows['AdministrationZip']?></td>
        <td><?php echo $rows['AdministrationCity']?></td>
        <td><?php echo $rows['AdministrationName']?></td>
        <td><?php echo $rows['LikedBy']?></td>
        <td><button type="button" id="like_btn">Like</button></td>
    </tr>
 <?php
  }
 ?>
 </tbody>
</table>

2 个答案:

答案 0 :(得分:1)

我认为$dbh->query($sql)是导致您出错的原因。变量$dbh保存着sqlsrv_connect()的结果,但是您可以将其用作PDO class变量。

这样更改代码(包括错误检查):

<?php
    $serverName = "192.168.8.8";
    $connectionOptions = array(
        "Database" => "GeoDynamics",
        "Uid" => "User",
        "PWD" => "Password"
    );
    //Establishes the connection
    $dbh = sqlsrv_connect($serverName, $connectionOptions);
    if ($dbh === false) {
        echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
        exit;
    }
    $sql = "SELECT * FROM Aanbest WHERE ReceiptLimitDate = '2018-12-03'  order by ImportDatum desc"; 
    $getResults = sqlsrv_query($dbh, $sql);
    if ($getResults === false) {
        echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
        exit;
    }
?>
<style type="text/css">
.tftable {font-size:12px;color:#333333;width:100%;border-width: 1px;border-color: #729ea5;border-collapse: collapse;}
.tftable th {font-size:12px;background-color:#acc8cc;border-width: 1px;padding: 8px;border-style: solid;border-color: #729ea5;text-align:left;}
.tftable tr {background-color:#d4e3e5;}
.tftable td {font-size:12px;border-width: 1px;padding: 8px;border-style: solid;border-color: #729ea5;}
.tftable tr:hover {background-color:#ffffff;}
</style>
<table class="tftable" border="1">
<thead>
    <tr>
    <th>ID</th>
    <th>Aanbestedingsdatum</th>
    <th>Klasse</th>
    <th>Omschrijving</th>
    <th>Postcode</th>
    <th>Stad</th>
    <th>Bestuur</th>
    <th>LikedBy?</th>
    <th>Like</th>
    </tr>
</thead>
<tbody>
<?php
    while ($rows = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
?>
    <tr>
        <td><?php echo $rows['ID']?></td>
        <td><?php echo $rows['ReceiptLimitDate']?></td>
        <td><?php echo $rows['Classes1']?></td>
        <td><?php echo $rows['Title']?></td>
        <td><?php echo $rows['AdministrationZip']?></td>
        <td><?php echo $rows['AdministrationCity']?></td>
        <td><?php echo $rows['AdministrationName']?></td>
        <td><?php echo $rows['LikedBy']?></td>
        <td><button type="button" id="like_btn">Like</button></td>
    </tr>
<?php
  }
?>
</tbody>
</table>

答案 1 :(得分:0)

您可能会看到所谓的“死亡白屏”。基本上,您的脚本在执行代码时会遇到一些错误,但是错误报告已关闭,因此它无提示地失败。 / p>

尝试将其附加到脚本的开头,看看是否可以看到实际的问题:

ini_set('display_errors', 'On');
ini_set('html_errors', 0);
error_reporting(E_ALL);

如果这样不起作用,请查看更全面的答案here,或咨询您的托管服务提供商有关php.ini设置的信息。