繁体   English   中英

PHP-Mysql:从第一行和第二行输出的 sql 中的 PHP 回显值

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM