php - 如何从sql中跳过一行?

标签 php html mysql database web

嗨,php 和 mysql 新手。试图找到如何按降序跳过 sql 表循环中的第一个或最后一个值?我想首先显示最近添加的行,但不显示最后添加的行。

<tr>
<?php
    $page=$_GET['pages'];    /* created in index.php(pages.php?pages=m_$title) */
    $nage="z_".$page;
    $query="SELECT * FROM `$nage` ORDER BY a_id DESC LIMIT 0, 1" ;
    $run=mysqli_query($conn,$query);
    while($row=mysqli_fetch_array($run)) {
        $page_title=$row['title'];
        $img_src=$row['img_src'];

        // $page_desc=$row['story'];

        echo  "<tr bgcolor='pink'>
                   <td style='width:40%'>
                       <div class='titleDivX'>
                           <img class='imgFrame'src='$img_src' alt='tasvir'/>
                           <h5> $page_title</h5>
                       </div>
                   </td>
                   <td bgcolor='yellow'>
                       <div class='titleDivX'> </div>
                   </td>
               </tr>";
    }
?>

最佳答案

您可以使用描述的mysqli_num_rows函数here 。如果您想查找第一行或最后一行,则可以替换这部分代码

$run=mysqli_query($conn,$query);
while($row=mysqli_fetch_array($run)) {

有了这个

$run=mysqli_query($conn,$query);
$rowsCount=mysqli_num_rows($run);
$count = 0;
while($row=mysqli_fetch_array($run)) {
    $count++;       
    $isFirst = ($count == 1) ? true : false;
    $isLast = ($count == $rowsCount) ? true : false;

然后您可以在 IF 语句中使用 $isFirst$isLast 变量。例如,如果您想跳过最后一行,则可以使用如下代码:

<?php
    $page=$_GET['pages'];    /* created in index.php(pages.php?pages=m_$title) */
    $nage="z_".$page;
    $query="SELECT * FROM `$nage` ORDER BY a_id DESC LIMIT 0, 1" ;
    $run=mysqli_query($conn,$query);
    $rowsCount=mysqli_num_rows($run);
    $count = 0;
    while($row=mysqli_fetch_array($run)) {
        $count++;       
        $isFirst = ($count == 1) ? true : false;
        $isLast = ($count == $rowsCount) ? true : false;

        if (!$isLast) {
            $page_title=$row['title'];
            $img_src=$row['img_src'];

            // $page_desc=$row['story'];

            echo  "<tr bgcolor='pink'>
                       <td style='width:40%'>
                           <div class='titleDivX'>
                               <img class='imgFrame'src='$img_src' alt='tasvir'/>
                               <h5> $page_title</h5>
                           </div>
                       </td>
                       <td bgcolor='yellow'>
                           <div class='titleDivX'> </div>
                       </td>
                   </tr>";
        }
    }
?>

关于php - 如何从sql中跳过一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39811727/

相关文章:

html - 让我的 Rails 表单正常工作时遇到问题

javascript - 使用流程图从数据库中检索数据

c# - 从不同路由器连接到 AWS 上的 RDS

php - 加载时显示页面

php - 如何检查虚假流量点击

PHP 警告 : pack(): Type H: illegal hex digit r error

mysql - 为什么我无法在此列上创建外键?

php - 如何从mysql数据创建图表? (使用谷歌可视化 API)

html - CSS anchor 内联 block 未对齐

jquery - 使用 jQuery 横向动画(滑动)一个 div