第二个MySQL查询不起作用

时间:2010-07-19 11:54:29

标签: php mysql

检查此代码:

            $select = mysql_query("SELECT * FROM nieuws ORDER BY id DESC LIMIT 1");
            while($row = mysql_fetch_assoc($select)) {
            $datum = $row['time'];
            $titel = $row['title'];
            $bericht = $row['message'];
            ?>
            <div class="entry">

                <span class="blue date"><?php echo "$datum"; ?></span>
                <h3><?php echo "$titel"; ?></h3>
                <p><?php echo "$bericht"; ?></p> <br />
            </div><!-- end of entry --> <?php } ?>
            <?php 
            $select2 = mysql_query("SELECT * FROM nieuws ORDER BY id DESC LIMI 1, 1");
            while($row2 = mysql_fetch_assoc($select2)) {
                $datum = $row2['time'];
                $titel = $row2['title'];
                $bericht = $row2['message'];
                ?>
            <div class="entry">
                <span class="green date"><?php echo "$datum"; ?> </span>
                <h3><?php echo "$titel"; ?></h3>
                <p><?php echo "$bericht"; ?></p>
            </div> <!-- end of entry --> <?php } ?>
        </div><!-- end of news --> 

第一个新闻项目正确显示, 到第二个我得到这个错误:

警告:mysql_fetch_assoc():提供的参数不是有效的MySQL结果资源

有什么问题?

4 个答案:

答案 0 :(得分:2)

SELECT * FROM nieuws ORDER BY id DESC LIMI 1, 1

应该是

SELECT * FROM nieuws ORDER BY id DESC LIMIT 1, 1

答案 1 :(得分:2)

你拼写错误,你错过了最后的T

答案 2 :(得分:1)

DESC LIMI - &gt; DESC LIMIT,一个错字。收听错误消息!

            $select2 = mysql_query("SELECT * FROM nieuws ORDER BY id DESC LIMI 1, 1");

答案 3 :(得分:1)

该错误消息表示您的SQL查询失败。你应该添加一些错误检查代码,以便在发生这种情况时显示mysql错误。

if( !$select2 ) {
  echo mysql_error();
}

我不会将其留在生产代码中,但它对调试代码非常有用。

阅读手册总是一个好主意:
http://www.php.net/manual/en/function.mysql-query.php
http://php.net/manual/en/function.mysql-error.php
http://www.php.net/manual/en/function.mysql-errno.php