[英]PHP-Mysql : Php echo values from sql out put from 1st and second row
我正在嘗試構建一個網頁(使用 css)來顯示今天和明天的值。 我可以成功獲取今天的值,但無法看到/獲取下一行的明天的值。 我需要我的網頁上的下一行Fajr Begins
(php 代碼)。
mysql查詢:
select * from prayers where Date IN (CURDATE(), DATE_ADD(CURDATE(), INTERVAL 1 DAY)) LIMIT 2;
輸出:
| Date | Day | Fajr Begins | Fajr Jamaat | Sunrise | Zohr Begins | Zohr Jamaat | Asr Begins | Asr Jamaat | Magrib Begins | Magrib Jamaat | Isha Begins | Isha Jamaat | Jumah 1 | Jumah 2 | Day of week |
+------------+------+-------------+-------------+---------+-------------+-------------+------------+------------+---------------+---------------+-------------+-------------+---------+---------+-------------+
| 2021-10-15 | Fri | 05:33 | 06:00 | 07:23 | 12:50 | * | 15:33 | 16:15 | 18:11 | 18:16 | 19:59 | | 13:20 | 14:15 |
| 2021-10-16 | Sat | 05:34 | 06:15 | 07:24 | 12:50 | 13:30 | 15:32 | 16:00 | 18:09 | 18:14 | 19:57 | | 13:20 | 14:15 |
+------------+------+-------------+-------------+---------+-------------+-------------+------------+------------+---------------+---------------+------------+
所有列名都相同,所以我不能給它們取別名(這很容易)。
這是我的 PHP 腳本(我只從 html 表的第一個條目中提取了一小段代碼):
<?php
include "dbConn.php"; // Using database connection file here
$records = mysqli_query($db,"select * from prayers where Date IN (CURDATE(),
DATE_ADD(CURDATE(), INTERVAL 1 DAY)) LIMIT 2;" ); // fetch data from database
while($data = mysqli_fetch_array($records))
{
?>
<div class="u-table u-table-responsive u-table-1">
<table class="u-table-entity">
<colgroup>
<col width="100%">
</colgroup>
<tbody class="u-table-body">
<tr style="height: 118px;">
<td class="u-align-center u-table-cell u-table-cell-1"><?php echo $data['Fajr
Begins']; ?></td>
</tr>
</tbody>
</table>
</div>
<div class="u-table u-table-responsive u-table-2">
<table class="u-table-entity">
<colgroup>
<col width="100%">
</colgroup>
<tbody class="u-table-body">
<tr style="height: 118px;">
<td class="u-align-center u-table-cell u-text-palette-3-base u-table-cell-2"><?php
echo $data['Fajr Jamaat']; ?></td>
</tr>
</tbody>
</table>
</div>
<div class="u-table u-table-responsive u-table-3">
<table class="u-table-entity">
<colgroup>
<col width="100%">
</colgroup>
<tbody class="u-table-body">
<tr style="height: 72px;">
**<td class="u-align-center u-table-cell u-text-palette-2-base u-table-cell-3">Tommorrow Data</td>**
</tr>
</tbody>
</table>
</div>
<?php
}
?>
<?php mysqli_close($db); // Close connection ?>
附上網頁。
我已經用盡了所有搜索選項,但找不到解決方案。 請高手幫忙!
您的查詢正在返回正確的數據,並且僅限於兩行。 當您開始創建 HTML 表格時,您的while
循環一次只讀取一行,因此您永遠看不到明天的數據。
但是,無法保證數據返回的順序,因此您需要添加ORDER BY Date ASC
子句。
由於您只返回兩行,因此不需要while
語句。 您只需要連續兩次獲取,如下所示:
<?php
include "dbConn.php"; // Using database connection file here
// Query updated to include an ORDER BY clause
$records = mysqli_query($db,"select * from prayers where Date IN (CURDATE(),
DATE_ADD(CURDATE(), INTERVAL 1 DAY)) ORDER BY Date ASC LIMIT 2;" ); // fetch data from database
// read first row with today's data
$todaysData = mysqli_fetch_array($records);
// Create table for today here
// Now read second row, with tomorrow's data.
$tomorrowData = mysqli_fetch_array($records);
// Create table for tomorrow here.
mysqli_close($db); // not strictly required since PHP will do this anyway
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.