【问题标题】:Fatal error: Call to a member function fetch_assoc() on resource [closed]致命错误:调用资源上的成员函数 fetch_assoc() [关闭]
【发布时间】:2014-12-26 20:48:03
【问题描述】:

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

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

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

【问题讨论】:

  • 尝试用“$sql2”替换“$result”
  • 好的,现在只剩下这个错误:“致命错误:在第 7 行的 '\index.php' 资源中调用成员函数 fetch_assoc()”
  • 整个 mysql_* 系列函数已弃用,在新代码中使用它们很危险且不鼓励。看看mysqli。至于错误,我会尝试mysql_fetch_assoc($sql2),但我不太确定,我已经好几年没用过这个API了
  • mysql(不是mysqli)甚至没有OO风格的api

标签: php mysql sql database undefined


【解决方案1】:

要解决您的问题,您需要使用 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

【讨论】:

  • 好吧,这行得通。谢谢!我将尝试将所有内容从 MySQL 更改为 MySQLi。 :)
猜你喜欢
  • 2016-08-30
  • 2012-09-26
  • 1970-01-01
  • 1970-01-01
  • 2018-06-16
  • 2011-07-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多