[英]PHP/MSSQL: query result to table
我正在嘗試將 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>
我認為$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>
您可能正在看到所謂的“死亡白屏”。 基本上,您的腳本在執行代碼時遇到了一些錯誤,但錯誤報告已關閉,因此它會靜默失敗。
嘗試將其附加到腳本的開頭,看看是否可以看到實際問題:
ini_set('display_errors', 'On');
ini_set('html_errors', 0);
error_reporting(E_ALL);
如果這不起作用,請在此處查看更全面的答案,或咨詢您的托管服務提供商有關其 php.ini 設置的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.