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
mysql_fetch_array is a PHP function that will allow you to access data stored in the result returned from the TRUE mysql_query if u want to know what is returned when you used the mysql_query function to query a Mysql database. It is not something that you can directly manipulate. Syntax.
mysqli_fetch_array() is an extended version of the mysqli_fetch_row() function. In addition to storing the data in the numeric indices of the result array, the mysqli_fetch_array() function can also store the data in associative indices, using the field names of the result set as keys.
Difference: mysql_fetch_assoc() will always assing a non-secuencial key (like "color" and not a number). mysql_fetch_array() will assing a number key if there are not "word" key (0 and not "color" if there are not "color") but, you can choose to assing of key with a parameter...
The fetch_row() / mysqli_fetch_row() function fetches one row from a result-set and returns it as an enumerated array.
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";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With