0

我的 while 语句有问题,它不会显示第一行,我不知道为什么,任何帮助将不胜感激。

 <?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>";
         }


    }


 ?>
4

2 回答 2

0

第一个结果由 mysql_fetch_array 消耗,因此当您开始循环时,您正在消耗第二个项目。试试这段代码,我基本上在循环结束时分配下一个值。

另外,我 100% 同意 Jason McCreary,我将评论您当前的查询受 SQL 注入影响。但是,这应该可以解决您的第一个问题,帮助您朝着最终正确的解决方案迈出一小步。

<?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>";
}
于 2013-05-16T19:44:12.630 回答
0
    if (mysql_num_rows($result)==0){
        $class = "hideMe";
...
于 2013-05-16T19:55:45.380 回答