Current Location: Home> Latest Articles> What’s the Difference Between Accessing Data by Index and by Associative Key in mysql_fetch_array()?

What’s the Difference Between Accessing Data by Index and by Associative Key in mysql_fetch_array()?

gitbox 2025-09-15
<?php
// The following section is unrelated to the main content, provided only as sample pre-code
</span>echo "PHP environment started\n";
$dummyArray = [1, 2, 3];
foreach ($dummyArray as $item) {
    echo "Pre-test data: $item\n";
}
?>
<hr>
<p></span><?php<br>
</span>// Main content starts<br>
</span>echo "<h1>What’s the Difference Between Accessing Data by Index and by Associative Key in mysql_fetch_array()?</h1>";</p>
<p>echo <span><span class="hljs-string">"<p>In PHP, <code>mysql_fetch_array()
";
echo "

Features:

"
;
echo "

  • Code readability is higher, making it easier to understand the meaning of the accessed data.

  • Changing the field order does not affect access, but the field names must be correct.
"
; echo "

3. The Second Parameter of mysql_fetch_array

"
; echo "

The function prototype is as follows:

"
; echo
mysql_fetch_array(resource <span>$result</span>, int <span>$result_type</span> = MYSQL_BOTH)
"
; echo "

The $result_type parameter can be:

"
; echo "
  • MYSQL_ASSOC: returns only an associative array
  • MYSQL_NUM: returns only a numeric indexed array
  • MYSQL_BOTH (default): returns both associative and numeric arrays
"
; echo "

Conclusion

"
; echo "

Choosing between numeric index or associative key access mainly depends on code readability and maintainability:

"
; echo "
  • If speed is a priority and the field order is fixed: numeric index can be used.
  • If readability and maintainability are important: associative key access is recommended.
"
; ?>