First record from database is not displayed?

纵饮孤独 提交于 2019-12-12 03:36:54

问题


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>';
    }
}

回答1:


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 '...';
    }
}



回答2:


Change your first lines to this:

$cnt = mysql_num_rows($result)

if (!$cnt) {
    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 {
....



回答3:


Your if() statement seems flawed. The statement:

$row = mysql_fetch_array($result);

will fetch a row and move the pointer in $result to the next row, so your while will start at the second row. If your query returns only one row, you're effectively calling mysql_fetch_array() twice and skipping over the data returned.

You should find some other way of checking if you have results (possibly with mysql_num_rows()).



来源:https://stackoverflow.com/questions/10923983/first-record-from-database-is-not-displayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!