douaonong7807 2011-03-07 23:30
浏览 31
已采纳

PHP循环永远不会结束

How come my while loop never ends? Can't get it to work, maybe there is something wrong with my class?

function getCategory() {

    require_once('includes/database/config.php');

    $database   = new Database();
    $database   ->sqlQuery("SELECT * FROM news_category");    
    $rows = $database   ->sqlNumRows();

    if($rows > 0) {
        while($row = $database->sqlGetRows()) {
            echo "<li><input type='hidden' value='" . $row->id . "' id='hiddenForm' /><span></span> <img src='gfx/close.png' alt='' title='Slett Kategorien' /></li>";
        }
    } else {
        echo "<hr>";
        echo "<br />";
        echo "<span>Det er ingen kategorier her enda!</span>";
    }

}

class Database {

    private $mysqli;
    private $query;
    private $string;

    public function __construct() {
        $this->mysqli = new mysqli('localhost', 'root', '', 'hltv');
    }

    public function sqlQuery($sql) {
        $this->mysqli->query($sql);
        $this->query = $sql;
    }

    public function sqlNumRows() {
        $q = $this->mysqli->query($this->query);
        return $q->num_rows;
    }

    public function sqlGetRows() {
        $q = $this->mysqli->query($this->query);
        return $q->fetch_object();
    }

}
  • 写回答

1条回答 默认 最新

  • dongying3830 2011-03-07 23:34
    关注

    Because you are always re-querying. Everytime you call #sqlGetRows, you execute the query again, and therefore you always get the first row from the query whenever you call it.

    What you could do is, for example:

    public function sqlQuery($sql) {
        $this->lastQuery = $this->mysqli->query($sql);
    }
    
    public function sqlGetRow() {
        $this->lastQuery->fetch_object();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 经gamit解算的cors站数据再经globk网平差得到的坐标做形变分析
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式