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

时间:2014-12-26 20:48:03

标签: php mysql sql database undefined

我遇到了这个问题,我无法开始工作。

<?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.");
            }
        ?>

我确定这些表格是正确的,只是,我无法列出这些信息。

1 个答案:

答案 0 :(得分:2)

要解决您的问题,您需要使用MySQL过程函数语法:

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

但是,您不应该使用MySQL。看看MySQLi:http://php.net/manual/en/book.mysqli.php和PDO:http://php.net/manual/en/book.pdo.php