dongpengyu1363
dongpengyu1363
2014-12-26 20:48
浏览 46
已采纳

致命错误:在资源[关闭]上调用成员函数fetch_assoc()

I got this problem, which I just can't get to work.

<?php
            $sql="SELECT * FROM `news_vertical`";
            $sql2=mysql_query($sql) or die("Couldn't etablish connection with the server or username wasn't found in the database.");
            $count=mysql_num_rows($sql2);
            if($count>0)
            {
                while($row = $sql2->fetch_assoc()) {
                    if($row["Pull-LR"] == 'Right')
                    {
                        echo '<hr class="featurette-divider"><div class="row featurette"><div class="col-md-7"><h2 class="featurette-heading">', $row["title"], '</h2><span class="text-muted">- Skrevet af: ', $row["userstamp"], '</span><p class="lead">', $row["text"], '</p></div><div class="col-md-5"><img class="featurette-image img-responsive" data-src="holder.js/500x500/auto" alt="500x500" src="', $row["picture_url"], '" data-holder-rendered="true"></div></div><div class="row featurette"><div class="col-md-2 pull-left"><p>Skrevet den: ', $row["timestamp"], '</p></div></div>';
                    }
                    else if($row["Pull-LR"] == 'Left')
                    {
                        echo '<hr class="featurette-divider"><div class="row featurette"><div class="col-md-5"><img class="featurette-image img-responsive" data-src="holder.js/500x500/auto" alt="500x500" src="', $row["picture_url"], '" data-holder-rendered="true"></div><div class="col-md-7"><h2 class="featurette-heading">', $row["title"], '</h2><span class="text-muted">- Skrevet af: ', $row["userstamp"], '</span><p class="lead">', $row["text"], '</p></div></div><div class="row featurette"><div class="col-md-2 pull-right"><p>Skrevet den: ', $row["timestamp"], '</p></div></div>';
                    }
                }
            }
            else
            {
                die("Could't find 'news_vertical' database.");
            }
        ?>

I'm sure the tables are correct, it's just, I can't get it to list the information.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanhu7400
    duanhu7400 2014-12-26 21:02
    已采纳

    To fix your problem, you need to use the MySQL procedural function syntax:

    while ($row = mysql_fetch_assoc($sql2)) {
        // Run your code
    }
    

    However, you shouldn't be using MySQL. Have a look at MySQLi: http://php.net/manual/en/book.mysqli.php and PDO: http://php.net/manual/en/book.pdo.php

    点赞 评论

相关推荐