mysql_fetch_assoc vs. mysql_fetch_array

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Locked
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

mysql_fetch_assoc vs. mysql_fetch_array

Post by icesolid »

I always use mysql_fetch_array($result) when running a query. I was just wondering is there any advantage (ex: speed) using mysql_fetch_assoc instead?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: mysql_fetch_assoc vs. mysql_fetch_array

Post by Christopher »

I doubt that there is any speed difference. The time is taken with the fetch, not PHP putting it into an array of one type or another.
(#10850)
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Re: mysql_fetch_assoc vs. mysql_fetch_array

Post by icesolid »

So using either one really makes no difference.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: mysql_fetch_assoc vs. mysql_fetch_array

Post by Luke »

Probably not any noticeable difference... although mysql_fetch_array MUST be at least somewhat slower because it does all of the work that mysql_fetch_assoc does PLUS a numeric array.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: mysql_fetch_assoc vs. mysql_fetch_array

Post by John Cartwright »

I'll be damned if anyone used speed as an argument in using on over another :twisted:
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: mysql_fetch_assoc vs. mysql_fetch_array

Post by Luke »

I sure as hell wouldnt LOL
devhen
Forum Newbie
Posts: 1
Joined: Thu May 21, 2009 5:15 pm

Re: mysql_fetch_assoc vs. mysql_fetch_array

Post by devhen »

mysql_fetch_assoc would be faster than mysql_fetch_array because mysql_fetch_array is populating numeric *and* associative arrays for each result. mysql_fetch_array($result, MYSQL_ASSOC) is equivalent to mysql_fetch_assoc. Furthermore, mysql_fetch_array($result, MYSQL_NUM) is the fastest because it is only populating a numeric array for each result. This makes it faster and uses less bandwidth.
John Cartwright wrote:I'll be damned if anyone used speed as an argument in using on over another :twisted:
John Cartwright has obviously never seen a result set larger than 10 million rows, let alone 1 billion, and he'll have to be damned because there is definitely a speed difference on very large result sets and I am using speed as an argument for using one over the other ;)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: mysql_fetch_assoc vs. mysql_fetch_array

Post by Benjamin »

Old Thread = Locked
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: mysql_fetch_assoc vs. mysql_fetch_array

Post by John Cartwright »

devhen wrote:mysql_fetch_assoc would be faster than mysql_fetch_array because mysql_fetch_array is populating numeric *and* associative arrays for each result. mysql_fetch_array($result, MYSQL_ASSOC) is equivalent to mysql_fetch_assoc. Furthermore, mysql_fetch_array($result, MYSQL_NUM) is the fastest because it is only populating a numeric array for each result. This makes it faster and uses less bandwidth.
John Cartwright wrote:I'll be damned if anyone used speed as an argument in using on over another :twisted:
John Cartwright has obviously never seen a result set larger than 10 million rows, let alone 1 billion, and he'll have to be damned because there is definitely a speed difference on very large result sets and I am using speed as an argument for using one over the other ;)
Sorry to reply to this old topic. However, you would be mistaken in making that assumption. Reguardless, you have to be absolutely mad to be iterating a result set of 10 million rows. :banghead:

EDIT | Double sorry, didn't realize it was locked!
Locked