【问题标题】:Selecting the highest ID from a database table with PHP使用 PHP 从数据库表中选择最高 ID
【发布时间】:2016-08-18 20:22:49
【问题描述】:

我正在尝试从数据库中选择具有最高 ID 的一行并将其回显到表中的页面。

我可以手动运行查询,它可以正常工作,但是当我尝试通过 PHP 动态执行时;这没用。我的代码有什么问题?

<?php               
    $sql_query = "SELECT * FROM single_user_orders ORDER BY order_id DESC LIMIT 1";
    $result = mysqli_query($dbconfig, $sql_query);
    $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
    $count = mysqli_num_rows($result);

    echo '<div class="row" style="margin-top: 30px;">';
        echo '<div class="col-md-12">';
            echo '<table class="table">';
            if($count > 1) {
                echo '<tr>';
                    echo '<th>Order ID</th>';
                    echo '<th>Status</th> ';
                    echo '<th>Order Total</th>';
                    echo '<th>Order Description</th>';
                echo '</tr>';
                while ($row = mysqli_fetch_array($result)) {
                    echo '<tr>';
                        echo '<td>'. $row['order_id'] .'</td>';
                        echo '<td>'. $row['status'] .'</td> ';
                        echo '<td>'. $row['order_total'] .'</td>';
                        echo '<td>'. $row['order_description'] .'</td>';
                    echo '</tr>';
                }
            }
            echo '</table>';
        echo '</div>';
    echo '</div>';
?>

【问题讨论】:

  • 你能描述一下it does not work吗?
  • 没有错误显示?
  • 没有错误,只是没有表格。
  • $count 的值是多少?
  • $count 的值为 1。

标签: php mysql


【解决方案1】:

您正在使用while 循环再次获取数据,因为不需要再次获取数据,因为它已经在开始时使用$row = mysqli_fetch_array($result, MYSQLI_ASSOC); 获取。

尝试关注

<?php
$sql_query = "SELECT * FROM single_user_orders ORDER BY order_id DESC LIMIT 1";
$result = mysqli_query($dbconfig, $sql_query);
$row = mysqli_fetch_assoc($result);
$count = mysqli_num_rows($result);

echo '<div class="row" style="margin-top: 30px;">';
echo '<div class="col-md-12">';
echo '<table class="table">';
if ($count >= 1)
{
    echo '<tr>';
    echo '<th>Order ID</th>';
    echo '<th>Status</th> ';
    echo '<th>Order Total</th>';
    echo '<th>Order Description</th>';
    echo '</tr>';
    // Removed while loop as data is already fetched
    // you just need to echo the values now
    echo '<tr>';
    echo '<td>' . $row['order_id'] . '</td>';
    echo '<td>' . $row['status'] . '</td> ';
    echo '<td>' . $row['order_total'] . '</td>';
    echo '<td>' . $row['order_description'] . '</td>';
    echo '</tr>';
}
echo '</table>';
echo '</div>';
echo '</div>';
?>

这里不需要while 循环,因为使用LIMIT 1 只重试了一行。如果检索不止一行,在这种情况下,while 循环将为您解决问题。

【讨论】:

    【解决方案2】:

    看看

    $sql_query = "SELECT * FROM single_user_orders ORDER BY order_id DESC LIMIT 1";
    

                if($count > 1) {
    

    您总是只有 1 行。尝试改变条件

    【讨论】:

      【解决方案3】:

      选择超过1个记录时才显示表。您的查询将返回最多一个结果,因此该表将永远不会显示。

      使用if($count &gt;= 1) 代替if($count &gt; 1)

      请注意“大于或等于”符号:&gt;=

      【讨论】:

      • 这仍然不起作用。数据库中有order_id 1、2和3。这是显示表格,但当我需要第 3 行时显示 1 和 2。
      【解决方案4】:

      用这个更新你的代码,因为有两个错误

      1) count >= 1
      
      2) while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {  //add MYSQLI_ASSOC
      
          }
      

      【讨论】:

      • 不需要这个,它是一行,所以为什么你会循环?
      • 但是哥们他用过 mysqli_fetch_array($result) 和 $row['order_id']
      【解决方案5】:

      您的结果集仅包含 1 行。 因此,mysqli_num_rows($result) 将返回 1 计数。所以你应该检查

      if($count==1) {
      ...
      }
      

      【讨论】:

        【解决方案6】:
        <?php               
            $sql_query = "SELECT * FROM single_user_orders ORDER BY order_id DESC LIMIT 1";
            $result = mysqli_query($dbconfig, $sql_query);
            $count = mysqli_num_rows($result);
        
            echo '<div class="row" style="margin-top: 30px;">';
                echo '<div class="col-md-12">';
                    echo '<table class="table">';
                    if($count >= 1) {
                        echo '<tr>';
                            echo '<th>Order ID</th>';
                            echo '<th>Status</th> ';
                            echo '<th>Order Total</th>';
                            echo '<th>Order Description</th>';
                        echo '</tr>';
                        while ($row = mysqli_fetch_array($result)) {
                            echo '<tr>';
                                echo '<td>'. $row['order_id'] .'</td>';
                                echo '<td>'. $row['status'] .'</td> ';
                                echo '<td>'. $row['order_total'] .'</td>';
                                echo '<td>'. $row['order_description'] .'</td>';
                            echo '</tr>';
                        }
                    }
                    echo '</table>';
                echo '</div>';
            echo '</div>';
        ?>
        

        1 个或多个订单试试这个

        【讨论】:

          猜你喜欢
          • 2023-03-22
          • 1970-01-01
          • 2022-08-17
          • 1970-01-01
          • 2013-04-26
          • 1970-01-01
          • 2012-03-16
          • 1970-01-01
          • 2012-10-08
          相关资源
          最近更新 更多