0

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.

1
  • Why you are using = instead of == in your while loop Commented Mar 2, 2014 at 23:01

2 Answers 2

6

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/>";
}
Sign up to request clarification or add additional context in comments.

2 Comments

Would you mind giving me an example or adding to my code? I am extremely new and understand what you are saying but don't know how to implement. Thank you.
@user982853 I added a rough example above. Something like that should work for you.
1

From php.net on mysql_fetch_row (my emphasis):

Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead.

What your current code does is to save only the first row to $DB->Results_Row, and in the while loop keep assigning that row to $R.

The intended usage is to repeatedly call mysql_fetch_row, do something with the return value, and exit the loop when it returns FALSE. What you can do is

  1. save $Results as an instance variable, create another method that calls mysql_fetch_row on that variable and call that from the while condition expression; or
  2. simply return $Results from your Query method and call mysql_fetch_row on that in the while condition expression.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.