dongzhan8001 2014-03-02 14: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 15: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部