mysql_fetch_array while loop. How does it work?

2019-06-22 17:59发布

I have read about the function on php.net and that has still not answered my question. I know a beginners amount of C and I've just started using php. Normally in C if you were to do a while loop there needs to be some condition to advance the loop to a point where it will no longer be valid like so:

while (x >= 10)  
{ 
    printf("..."; 
    printf("x \n"; 
    x++; 
}

However in my php script that I'm using for a pm message system I have a while loop like this:

while($row2 = mysql_fetch_array($query))

followed by:

{ 
  echo "<table border=1>";
  echo "<tr><td>";
  echo "Message #: ";
  echo $row['id'];
  echo "</td></tr>";
  echo "<tr><td>";
  echo "To: ";
  echo $row['to'];
  echo "</td></tr>";
  echo "<tr><td>";
  echo "From: ";
  echo $row['from'];
  echo " ";
  echo "</td></tr>";
  echo "<tr><td>";
  echo "Message: ";
  echo $row['message'];
  echo "</td></tr>";
  echo "</br>";
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
<tr><td colspan=2></td></tr>
<tr><td></td><td>
<input type="hidden" name="id" maxlength="32" value = "<?php echo $row['id']; ?>">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="delete" value="Delete PM # <?php echo $row['id']; ?>">
</td>
<td colspan="2" align="right">
<input type="submit" name="reply" value="Reply to <?php echo $row['from']; ?>">
</td></tr>
</table>
<?php } ?>

How exactly does this work, from a C background it would seem almost like this would stay in the same spot, printing out the same "array fetch" 'row' from the same "$query" every time we go through the loop....

Is there a way I can write this to give me a better logical understanding of whats going on? like saying:

$i=0;
while ($row = ($i+mysql_fetch_array($query)) {
...
...
$i++;}

I know that probably wont work but how does this function increment itself? And is there a way to write it where it would actually have some sort of incrementation visible in the code?

Thank you

4条回答
戒情不戒烟
2楼-- · 2019-06-22 18:19

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.

查看更多
淡お忘
3楼-- · 2019-06-22 18:23

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

查看更多
Melony?
4楼-- · 2019-06-22 18:32

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.

查看更多
一纸荒年 Trace。
5楼-- · 2019-06-22 18:38

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";
}
查看更多
登录 后发表回答