Jump to content

fetch_array or fetch_assoc?


GingerRobot

Recommended Posts

Umm, i was wondering what the differance between mysql_fetch_array and mysql_fetch_assoc is and was looking it up on the manual...it says that with assoc() you just get the assoicative indices, but with array() it provides it with both associative and numerical.

No particular problem, but im wondering what the point of having both is? Is one more efficient? Should one be used instead of the other in certain situations? It seems to be that assoc() is basically pointless a using array() does all it does and more, but does that make it slower?
Link to comment
Share on other sites

I've stopped using mysql_fetch_array() and use mysql_fetch_assoc() all the time. Accessing the row array via an associative index makes your code easier to understand and almost self-documenting. It's much easier to understand what someone's code is doing when they reference $row['userid'] vs $row[0].

When you use mysql_fetch_array() it will return two entries for each field, so doing a foreach on the array is non-trivial as is a regular for loop.

Ken
Link to comment
Share on other sites

Ah ok, i think i see what you mean having just created a table with 3 fields and got the following output from printing the array from mysql_fetch_array:

Array ( [0] => 0 [id] => 0 [1] => bob [first] => bob [2] => jones [last] => jones )

So basically if you use mysql_fetch_array() rather than mysql_fetch_assoc() the array will contain twice as many items. I thought perhaps it was being clever and somehow had two keys for each value.

Thanks for your help
Link to comment
Share on other sites

No. With mysql_fetch_array () you can specify whether you want to to be assoc or num. the default value is both.

result_type

    The type of array that is to be fetched. It's a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and the default value of MYSQL_BOTH.
Link to comment
Share on other sites

[quote author=kenrbnsn link=topic=99823.msg393304#msg393304 date=1152307834]
It's much easier to understand what someone's code is doing when they reference $row['userid'] vs $row[0].
[/quote]

How styles differ !

I prefer
[code]<?php
$res = mysql_query("SELECT id, name FROM tablename")
while (list ($id, $name) = mysql_fetch_row($res)) {

    // as it makes it simpler and more readable then to do this
    echo "<option value='$id' > $name </option>\n";
}
?>[/code]
Note: NOT "select * "
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.