开发者

Mysql/PHP not displaying all rows

Im creating a simple message board with php/mysql. Users enter their name and a message, javascript displays the message immediately, and php/mysql stores it in a database. When the page loads, it should display all the messages in the database in a formatted list.

However, it seems that my php is displaying only some of the messages arbitrarily. They are in the proper chronological order but some are missing and it only displays 4 of them. Manually looking at the entries in the database, I can see that the posted messages are indeed being stored in their table. They're just not ALL being displayed. wierd.

Heres the HTML/PHP that displays the messages:

<?php 
    $records = getMessages(); //see getMessages() function below
    $names = $records["names"];
    $messages = $records["messages"];
    $dates = $records["dates"];

    for($i = count($records); $i > 0; $i--){ ?>
    <div class="message">
        <p class="message_txt"><?php echo $messages[$i];?></p>
        <div>
            <div class="message_name">
                <?php echo $names[$i];?>
            </div>
            <div class="message_date">
                <small>
            <?php 
                echo "Posted on ";
                echo date("F j, Y",strtotime($dates[$i]));
        ?>
        </small>
        </div>
    </div>
</div>
<?php } ?>

Heres the getMessages() function 开发者_开发问答from above:

function getMessages(){
    $conn = connect("wedding");
    $ids;
    $names;
    $messages;
    $dates;
    $get_messages_query = "SELECT id, name, message, date 
               FROM messages;";
    $get_messages_result = mysql_query($get_messages_query,$conn) or die(mysql_error());
    $i = 0;
    while($row = mysql_fetch_array($get_messages_result)){
    $ids[$i] = $row["id"];
    $names[$i] = $row["name"];
    $messages[$i] = $row["message"];
    $dates[$i] = $row["date"];
    $i++;
    }

    $entries = array("ids" => $ids,
                     "names" => $names,
                     "messages" => $messages,
                     "dates" => $dates
               ); 
    return $entries;
}

And this is the output:

<div class="message"> 
    <p class="message_txt">Yo this is a message</p> 
    <div> 
    <div class="message_name">Bob</div> 
    <div class="message_date"><small>Posted on September 18, 2010</small></div> 
    </div>                  
</div> 
<div class="message"> 
    <p class="message_txt">This is a message another</p> 
    <div> 
    <div class="message_name">Andrew</div> 
    <div class="message_date"><small>Posted on September 6, 2010</small></div> 
    </div> 
</div> 
<div class="message"> 
    <p class="message_txt">And another message</p> 
    <div> 
    <div class="message_name">Andrew</div> 
    <div class="message_date"><small>Posted on September 6, 2010</small></div> 
    </div> 
</div> 
<div class="message"> 
    <p class="message_txt">This is a message</p> 
    <div> 
    <div class="message_name">Andrew</div> 
    <div class="message_date"><small>Posted on August 27, 2010</small></div> 
    </div> 
</div> 

Im not sure whats going on here. It seems simple enough. I suppose it might be some small idiotic error I can't see... but I can't see it.

Any assistance would be appreciated.


The reason is that you're only looping to 4 explicitly:

 for($i = count($records);

Records is the variable holding the 4 arrays (ids, names, messages, dates). Each array likely holds the correct number of messages, but you're bounding your loop on records.

Other Considerations for next time:

  • modify your SQL statement to order these records explicitly. You may see them NOW in the order you expect, but it's not guaranteed by any RDBMS if you don't explicitly state the order you wish. Perhaps: SELECT ... FROM... ORDER BY date DESC?

  • The for loop is starting at the bottom. In conjunction with an explicit ORDER BY, consider using

    for($i = 0; i<=count($records);  $i++)
    
  • echo out the count of each array (names, messages, dates). Does that jive with what you expected back from your DB query?


I suggest adding 2 var_dump() functions (or fb() if you use FirePHP) in the getMessages() function to see what's going on.

function getMessages(){
    $conn = connect("wedding");
    $ids;
    $names;
    $messages;
    $dates;
    $get_messages_query = "SELECT id, name, message, date 
               FROM messages;";
    $get_messages_result = mysql_query($get_messages_query,$conn) or die(mysql_error());
    $i = 0;
    while($row = mysql_fetch_array($get_messages_result)){
    $ids[$i] = $row["id"];
    $names[$i] = $row["name"];
    $messages[$i] = $row["message"];
    $dates[$i] = $row["date"];
    $i++;var_dump($i);
    }

    $entries = array("ids" => $ids,
                     "names" => $names,
                     "messages" => $messages,
                     "dates" => $dates
               ); var_dump($entries);
    return $entries;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜