dongzhan8001 2014-03-02 22:59 采纳率: 0%
浏览 62
已采纳

循环中的PHP - 没有停止

I am using a php while loop to read through results but it just loops continuously over the first record and never moves to the second record and never stops. My code:

class MySqlDatabase {   

    public function __construct() {
        $this->Open_Connection();
    }

    //Vars for Function Open_Connection
    private $_Connection;
    //Function Open_Connection - Connects to DB
    public function Open_Connection(){
        $this->_Connection = mysqli_connect('localhost','xxx','xxx','xxx');
        if(!$this->_Connection){
            die("Connection Failed: ". mysqli_error($this->_Connection));
        } else {
            echo "Success";
        }
    }

    //Vars for Function Query
    public $Results_Row;
    //Function Query - Runs Query and returns results 
    public function Query($Sql){
        $Results = mysqli_query($this->_Connection, $Sql);
        if(!$Results){
            die("Query Failed: ". mysqli_error($Results). "<br/>".$Sql);
        }
        $this->Results_Row = mysqli_fetch_row($Results);

        return $Results;
    }

}

$Db = new MySqlDatabase;

$Db->Query("SELECT * FROM Users");

while ($R = $Db->Results_Row){
    var_dump($R);
    echo "<hr/>";
}

This is creating an infinite loop of the first record that never stops. There are only two records in my db so it should stop after looping through the two results. Again it just keep infinitely looping the first record and never moves to the second record and then stops.Please help, thank you.

  • 写回答

2条回答 默认 最新

  • duansaxf095988 2014-03-02 23:00
    关注

    You never actually fetch a new row. You run your query, then access the first row, then just refer to that row over and over. You need to add a method to your class to fetch a row.

    You need something like this (untested code, based on what you have above):

    class MySqlDatabase {   
    
        public function __construct() {
            $this->Open_Connection();
        }
    
        //Vars for Function Open_Connection
        private $_Connection;
        //Function Open_Connection - Connects to DB
        public function Open_Connection(){
            $this->_Connection = mysqli_connect('localhost','xxx','xxx','xxx');
            if(!$this->_Connection){
                die("Connection Failed: ". mysqli_error($this->_Connection));
            } else {
                echo "Success";
            }
        }
    
        public function FetchRow() { // I added this
            $this->Results_Row = mysqli_fetch_row($Results);
        }
    
        //Vars for Function Query
        public $Results_Row;
        //Function Query - Runs Query and returns results 
        public function Query($Sql){
            $Results = mysqli_query($this->_Connection, $Sql);
            if(!$Results){
                die("Query Failed: ". mysqli_error($Results). "<br/>".$Sql);
            }
            // I removed the mysqli_fetch_row() here to method FetchRow()
    
            return $Results;
        }
    
    }
    
    $Db = new MySqlDatabase;
    
    $Db->Query("SELECT * FROM Users");
    
    while ($R = $Db->FetchRow()){ // I changed this
        var_dump($R);
        echo "<hr/>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line