Forum Moderators: coopster

Message Too Old, No Replies

mysql_fetch_array: How does it work?

and how is it different than mysql_fetch_row?

         

grnidone

6:22 pm on May 13, 2003 (gmt 0)



I don't understand the difference between 'mysql_fetch_array' and mysql_fetch_row.

I understand they both pull 'arrays' -- another term for lists -- from a database. But I am confused as to which list they are talking about.

If you look at the commands themselves, it seems fetching an array is fetching the entire database as a list and fetching a row is just fetching the next row with the dataset in each column as the list.

So, if you have a database with

ID......ColumnA.....ColumnB
-------------------------------------
1........dataA............dataB
2........dataC...........dataD

It seems like Fetch_row' should just get you a list like this:

1,dataA,dataB

and 'Fetch_array' get you a list like this:

1,dataA, dataB
2,dataC, dataD

But I don't think that is the case. Can someone clarify this?

jatar_k

6:32 pm on May 13, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



[php.net...]

mysql_fetch_array() is an extended version of mysql_fetch_row(). In addition to storing the data in the numeric indices of the result array, it also stores the data in associative indices, using the field names as keys.

An important thing to note is that using mysql_fetch_array() is not significantly slower than using mysql_fetch_row(), while it provides a significant added value.

The optional second argument result_type in mysql_fetch_array() is a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH ..... MYSQL_BOTH is the default for this argument

mysql_fetch_assoc() and mysql_fetch_row() each have a specific function, select as associative array or with numeric indices.

mysql_fetch_array gives you the most flexibilty, you can also force it to act like either of the above by specifying the result_type in the function call.

It falls more into the how you do things category I think. fetch_array is the most versatile but you could do any of them and acheive the same result.

<added>all three functions when called once will return one row of data.

grnidone

7:05 pm on May 14, 2003 (gmt 0)



So, is mysql_fetch_row deprecated?

Or is there a specific instance when one is clearly better than the other?

jatar_k

7:27 pm on May 14, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



not deprecated, just different.

I use mysql_fetch_array all the time, you might see a minute increase in speed if you use fetch_row with large datasets but aside from that the only difference is the format of the returned array.

daisho

8:35 pm on May 14, 2003 (gmt 0)

10+ Year Member



mysql_fetch_row($res) is the same as doing mysql_fetch_array($res,MYSQL_NUM)