Mysql_fetch_array while loop. How it works?

I read about the function on php.net and this has not yet answered my question. I know newbies in C, and I just started using php. Usually in C, if you have to do a while loop, there must be some condition to push the loop to the point where it will no longer be valid:

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

However, in my php script that I use for the pm messaging system, I have a while loop as follows:

 while($row2 = mysql_fetch_array($query)) 

and then:

 { 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 this work, from the C-background, it would seem, almost the same, would remain in the same place, printing the same "array selection" "string" from the same "query $" every time we go through the loop ....

Is there a way to write this to better understand the logical understanding of what is happening? as the saying goes:

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

I know that probably doesn't work, but how does this function increase? And is there a way to write it where it really will have some kind of increment visible in the code?

thanks

+4
source share
4 answers

Each time you call mysql_fetch_array , it pulls the next line from your query. This while keeps true, and mysql_fetch_array still has something that can be assigned to the $row2 variable. As soon as it leaves the lines, it has nothing to leave a variable, and false returns.

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

+10
source

You can think of mysql_fetch_array() as being similar to a file reader function that will return a new line every time and return EOF when it ends. Instead of EOF, mysql_fetch_array() returns FALSE (0).

In a boolean expression, PHP evaluates any non-0 value as TRUE, so the simplest syntax below goes through a loop, each time assigning a line to the $ row variable until we reach the end of the data set:

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

The PHP IDE will really complain about being able to, and that is how you should write the same code to avoid 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.

+1
source

Perhaps this next function can give you a little description of how mysql_fetch_array works during a loop?

 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"; } 
+1
source

mysql_fetch_array () is a function that you should call if you want to get a string from the given executable query, so you can describe it with mysql_fetch_array - select the result string as an associative array, a numeric array, or both

The scenario is this: it returns an array corresponding to the selected row and moves the pointer of the internal data forward. Additional Information:

 $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 of mysql_fetch_array does it use to work?

Let me answer this, 1) $ result is a given variable from mysql_query (), and this function returns the results obtained as the result of mysql , and this type can only be created when 7 functions are called:

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

and can be used by several functions, such as mysql_fetch_array() and mysql_db_name() and others

2) Function description: array mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] ) see mysql_fetch_array Doc

0
source

Source: https://habr.com/ru/post/1411356/


All Articles