Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

php - First record from database is not displayed?

Trying to work on a clients site and I am having a bit of difficulty. When I have no entries in the database, it catches at if(!row) and displays the message. This part works fine. My issue is when I have entries in the db, they do not display. I know the while loop works because I have several pages running a similar loop. In fact, this loop was copied from another page that displays this entry's information on a public page.

I know this site is mainly for questions, but I think I just need a fresh pair of eyes to look at my code(I've been coding for over 12 hours and I'm a bit tired). A lot of the code below is from a previous web designer and if it were up to me, I would just rewrite the entire site because the code is "out of date", but the client just wants me to improve on it. Any help would be greatly apprecieated.

$row = mysql_fetch_array($result);

if (!$row) {
    echo '<tr><td bgcolor="ffffff" colspan="3"><font face="arial,helvetica" size="2" color="000000">There are no entries at this time, check back later.</font></td></tr>';
} else {
    while ($row = mysql_fetch_array($result)) {

        echo '<tr>
                    <td bgcolor="ffffff"><font face="arial,helvetica" size="2" color="000000">$date - $row["theme"]</font></td>
                    <td bgcolor="ffffff" align="center">
                    <form action="dsp_modifyposition.php">
                        <input type="hidden" name="specialID" value="$row["specialID"]">
                        <input type="hidden" name="theme" value="$row["theme"]">
                        <input type="submit" value="  Modify  ">
                    </form>
                    </td>
                    <td bgcolor="ffffff" align="center">
                    <form action="act_deleteposition.php" onsubmit="return confirm('Are you sure you want to delete this event: $date ')">
                        <input type="hidden" name="specialID" value="$row["specialID"]">
                        <input type="hidden" name="theme" value="$row["theme"]">
                        <input type="submit" value="  Delete  ">
                    </form>
                    </td>
                </tr>';
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When you call mysql_fetch_array for the first time, the mysql result pointer is moved to the next row. Because nothing is done with this row, this first row does not get displayed. What you want is mysql_num_rows to check how many rows are in the resultset. As a side-note, I would suggest using mysql_fetch_assoc if you're not using the numeric indices.

if (!mysql_num_rows($result)) {
    echo '...';
} else {
    while ($row = mysql_fetch_assoc($result)) {
        echo '...';
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...