Using the mysql_fetch_array function

It is common to see an entry like this:

while($x = mysql_fetch_array($z)){}

Please tell me what is assigned to $x?


Answer 1, authority 100%

$x is assigned a string from the database selection, displayed as an array. At each iteration, the next one.

Example:

id name info
1 john teacher
2 bill programmer
3 hank bomzh
  1. $x = array('1','id'=>'1','john','name'=>'john','teacher','info'=>'teacher');
  2. $x = array('2','id'=>'2','bill','name'=>'bill','programmer','info'=>'programmer');
  3. $x = array('3','id'=>'3','hank','name'=>'hank','bomzh','info'=>'bomzh');

To avoid different array indexes with the same values, you need to choose some type of index.

$type = MYSQL_BOTH //     . -  
$type = MYSQL_ASSOC //  .      
$type = MYSQL_NUM //  .     ,   0
mysql_fetch_array($z,$type);

Answer 2, authority 50%

The next result (row) from mysql is assigned

$resource = mysql_query('SELECT `id`, `name` FROM `table` WHERE id>0;');
while ($x = mysql_fetch_array($resource)) {
  echo 'ID = '.$x['id'].'<br />';
  echo 'NAME = '.$x['name'].'<br />';
  echo '<br />';
  }