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 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题