mysql_fetch_array while loop. How does it work?

元气小坏坏 提交于 2019-11-29 14:37:12

Every time you call mysql_fetch_array it pulls the next row from your query. That while loop keeps returning true while the mysql_fetch_array still has something left to assign to the variable $row2. Once it's out of rows, it has nothing left to give the variable, and false is returned.

ETA: Regarding the last bit you mentioned, you can have a variable increment in each iteration of the loop like in your example, but it's not entirely necessary. You can also just see how many rows have been returned by doing something like $var = mysql_num_rows($data) before your while loop.

maybe this following function can give you a little description about "how mysql_fetch_array while loop work ?"

class test {
    public $plus = -1;
    public $data = array();
    public $return = array();
    function fetcharray(){
        $this->plus++;
        for($i = 0; $i < sizeof($this->data[$this->plus]); $i++){
            $this->return[$i] = $this->data[$this->plus][$i];
        }
        if(sizeof($this->data[$this->plus]) < sizeof($this->data[($this->plus - 1)])){
            for($i = sizeof($this->data[$this->plus]); $i < sizeof($this->data[($this->plus - 1)]); $i++){
                $this->return[$i] = "";
            }
        }
        return ($this->data[$this->plus]) ? $this->return : false;
    }
}
$test = new test();
$test->data = array(
    array("data00","data01","data02","data03","data04","data05"),
    array("data10","data11","data12","data13","data04","data15"),
    array("data20","data21","data22","data23","data24","data25"),
    array("data30","data31","data32","data33","data34","data35"),
    array("data40","data41","data42","data43","data44","data45")
);
while($array = $test->fetcharray()){
    echo $array[0]." = ".$array[1]." = ".$array[2]." = ".$array[3]." = ".$array[4]." = ".$array[5]."<br />\n";
}

You can think of mysql_fetch_array() as similar to a file read function that will return return a new row each time and return EOF when it hits the end. Instead of EOF though, mysql_fetch_array() returns FALSE(0).

In a Boolean expression, PHP evaluates any non-0 value as TRUE, so the simple syntax below loops through, assigning the row to the $row variable each time until we reach the end of the dataset:

while($row = mysql_fetch_array($query)) {
  // Process row
  ...
}

PHP IDEs will actually complain about an assignment in a condition, and this is how you should write the same code to avoid the notification:

// Get first row, or false if there were no rows
$row = mysql_fetch_array($query);
while ($row) {
  // Process row
  ...
  // Get next row
  $row = mysql_fetch_array($query);
}

Perhaps this structure looks more familiar.

mysql_fetch_array() is a function that you must call if you want to get a row from the resulted executed query, so you can describe it "mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both"

The Scenario is this : It returns an array that corresponds to the fetched row and moves the internal data pointer ahead. More Clarification :

$result = mysql_query("SELECT id, name FROM mytable");
$row = mysql_fetch_array($result, MYSQL_NUM);

1) What is the type of $result ? 2) What type mysql_fetch_array uses to work ?

Let me answer this, 1) $result is a resulted variable from mysql_query(), and this function returns results typed as mysql result and this type can only be created by calling 7 functions :

mysql_db_query(), mysql_list_dbs(), mysql_list_fields(), mysql_list_processes(), mysql_list_tables(), mysql_query(), mysql_unbuffered_query()

and can used by several functions like mysql_fetch_array() and mysql_db_name() and others

2)The description of the function is : array mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] ) see mysql_fetch_array Doc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!