PHP虽然不会返回第一行

时间:2013-05-16 19:40:57

标签: php sql if-statement while-loop row

我的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>";
         }


    }


 ?>

2 个答案:

答案 0 :(得分: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>";
}

答案 1 :(得分:0)

    if (mysql_num_rows($result)==0){
        $class = "hideMe";
...