不显示数据库中的第一条记录? [英] First record from database is not displayed?

查看:52
本文介绍了不显示数据库中的第一条记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在客户网站上工作时遇到了一些困难.当我在数据库中没有条目时,它会在 if(!row) 处捕获并显示消息.这部分工作正常.我的问题是当我在数据库中有条目时,它们不显示.我知道 while 循环有效,因为我有几个页面在运行类似的循环.事实上,这个循环是从另一个页面复制而来的,该页面在公共页面上显示了该条目的信息.

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.

我知道这个网站主要是为了提问,但我想我只需要一双新的眼睛来看看我的代码(我已经编码超过 12 个小时了,我有点累了).下面的很多代码来自以前的网页设计师,如果由我决定,我会重写整个网站,因为代码过时",但客户只是希望我对其进行改进.任何帮助将不胜感激.

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

推荐答案

第一次调用 mysql_fetch_array 时,mysql 结果指针移动到下一行.因为这一行没有做任何事情,所以不会显示第一行.您想要的是 mysql_num_rows 来检查结果集中有多少行.作为旁注,如果您不使用数字索引,我建议您使用 mysql_fetch_assoc.

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

这篇关于不显示数据库中的第一条记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆