donglu3184 2013-05-16 19:40
浏览 18
已采纳

PHP虽然不会返回第一行

got a problem with my while statement, it will not display the first row & I have no idea why, any help would be appreciated.

 <?php
      $user = $_SESSION['username'];
      $result = mysql_query("SELECT *FROM bookwrite WHERE username = '$user' ", $connection);


    if (mysql_fetch_array($result)==0){
        $class = "hideMe";
        $firstnameResult = mysql_query("SELECT * FROM users WHERE username = '$user' ", $connection);

        $row2 = mysql_fetch_array($firstnameResult);
        echo "<div class=\"platOptions welcome\"><div class=\"welcomeinfo\"><h1>Welcome!</h1>Welcome to Little Quill, ".$row2["firstName"].". We see you are new to the platform. To get started by adding your first post please click the \"New Entry\" button below <a class=\"newentryBTN\" href=\"newEntry.php\">New Entry</a></div></div>";

    } else {
        $class = "";
        while ($row = mysql_fetch_array($result)) {
            echo nl2br("<li class=\"editable\" id=\"".$row["id"]."\">"."<div class=\"entryInfo clearfix\">"."<div class=\"statusWrapper\"><div class=\"status\"></div><div class=\"statusIcon\"></div></div>"."<h1>".$row["post_title"]."</h1>"."<div class=\"timeline-date entryDate\">".$row["post_date"]."</div>"."</div>"."<p contentEditable=\"true\">".$row["post_details"])."</p><a class=\"deleteBTN\"href='delete.php?id=".$row["id"]."'>x</a></li>";
         }


    }


 ?>
  • 写回答

2条回答 默认 最新

  • doumeitang572461 2013-05-16 19:44
    关注

    The first result is consumed by mysql_fetch_array, so when you start your loop you are consuming the second item. Try this code out, I basically assign the next value, at the end of the loop.

    Also, I 100% agree with Jason McCreary, and I will make the comment that your current query is subject to SQL Injection. However, this should solve your first problem, helping you take a baby step towards to solution that's ultimately correct.

    <?php
    $user = $_SESSION['username'];
    $result = mysql_query("SELECT * FROM bookwrite WHERE username = '$user' ", $connection);
    
    $bookwrite_row = mysql_fetch_array($result);
    
    if($bookwrite_row) {
        while($bookwrite_row) {
            echo nl2br("<li class=\"editable\" id=\"".$row["id"]."\">"."<div class=\"entryInfo clearfix\">"."<div class=\"statusWrapper\"><div class=\"status\"></div><div class=\"statusIcon\"></div></div>"."<h1>".$row["post_title"]."</h1>"."<div class=\"timeline-date entryDate\">".$row["post_date"]."</div>"."</div>"."<p contentEditable=\"true\">".$row["post_details"])."</p><a class=\"deleteBTN\"href='delete.php?id=".$row["id"]."'>x</a></li>";
    
            $bookwrite_row = mysql_fetch_array($result);
        }
    } else {
        $class = "hideMe";
        $firstnameResult = mysql_query("SELECT * FROM users WHERE username = '$user' ", $connection);
    
        $row2 = mysql_fetch_array($firstnameResult);
        echo "<div class=\"platOptions welcome\"><div class=\"welcomeinfo\"><h1>Welcome!</h1>Welcome to Little Quill, ".$row2["firstName"].". We see you are new to the platform. To get started by adding your first post please click the \"New Entry\" button below <a class=\"newentryBTN\" href=\"newEntry.php\">New Entry</a></div></div>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog