?
Solved

Difference between fetch_array, fetch_assoc

Posted on 2007-07-29
7
Medium Priority
?
4,379 Views
Last Modified: 2008-11-26
:-)
As the subject says, whats the difference between using
mysql_fetch_array
and
mysql_fetch_assoc

From what I can tell, both can do this
$r = mysql_query("SELECT id FROM something");
While ($d = mysql_fetch_array) { //or mysql_fetch_assoc
 echo $d['id'];
}

so....
0
Comment
Question by:wildzero
  • 4
  • 2
7 Comments
 
LVL 4

Expert Comment

by:cg_medistox
ID: 19589073
There is absoltely no difference, if you passed the value MYSQL_NUM as the second parameter to the mysql_fetch_array then it would make a difference. Associated array means that it returns the columns to use as the index for the array, and using MYSQL_NUM means it uses the actual index as a number of the row

Two useful articles
http://uk.php.net/manual/en/function.mysql-fetch-array.php
http://uk.php.net/manual/en/function.mysql-fetch-assoc.php
0
 
LVL 11

Accepted Solution

by:
AlexanderR earned 1200 total points
ID: 19589124
I say difference is quite big and provides different levels of usefullness at the right times.

mysql_fetch_array gets data from database in BOTH numeric AND associative arrays.

mysql_fetch_assoc gets data in the form of associate array ONLY.

Lets use this database structure for clarification.

tbl_events
--------------
event_id
event_date
event_details

Some data:
event_id = 1
event_date = 1/06/2007
event_details = "First day of june"

$r = mysql_query("SELECT event_id, event_date, event_details FROM tbl_events");

doing fetch array first:

While ($d = mysql_fetch_array($r)) {
 echo $d['id'];
 print_r($d);
}

Note the output of print_r!!
Every data in database is outputed twice in different formats so your array looks like this:
$d[0]=1
$d["event_id"]=1
$d[1]=1/06/2007
$d["event_date"] = 1/06/2007
$d[2]="First day of june"
$d["event_details"] = "First day of june"

Now trying fetch_assoc

While ($d = mysql_fetch_array($r)) {
 echo $d['id'];
 print_r($d);
}

Note print_r difference!!
$d["event_id"]=1
$d["event_date"] = 1/06/2007
$d["event_details"] = "First day of june"

Now you should see why you got the same result both times in your original code. In both cases there is a member in array called _id

There is another important function you should ask about
mysql_fetch_row which outputs only numeric arrays so your $d would become
$d[0] = 1
$d[1] = 1/06/2007
$d[2]="First day of june"
0
 
LVL 11

Expert Comment

by:AlexanderR
ID: 19589161
Of course its out to be

Now trying fetch_assoc

While ($d = mysql_fetch_assoc($r)) {
 echo $d['id'];
 print_r($d);
}

Sorry.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 10

Author Comment

by:wildzero
ID: 19589241
Mmm I wonder if there is any performance difference when returning large sets of data...
perhaps a speed test is in order :-)
0
 
LVL 11

Assisted Solution

by:AlexanderR
AlexanderR earned 1200 total points
ID: 19589291
Here are some benchmarks taken from a database with 77 rows and 14 columns.  All values are in seconds
fetch_array
0.1581
0.0626
0.0994
0.1278
0.1495
------
average: 0.11948

fetch_assoc
0.0868
0.0477
0.0922
0.0044
0.0574
------
average: 0.0577

fetch_row
0.0330
0.0273
0.0573
0.0446
0.0455
------
average: 0.04156


At first glance fetch_array definetly maxes out but if you to run it again:
0.0636
0.0583
0.0452
0.0452
0.0249
------
average 0.04744

I guess it started to cash or smth.
Performance in terms of speed i wouldnt say would be any difference. Database got to do look through the whole thing which ever way you put it.  Now memory usage for the final output could be affected.   Not much though, unless you are talking about millions of records.
0
 
LVL 11

Expert Comment

by:AlexanderR
ID: 19589304
Sorry forgot to save the file after i change back to fetch_array.
Benchmarks are high as before.
0.0788
0.1195
0.0530
0.0937
0.1374
------
0.09648
0
 
LVL 10

Author Comment

by:wildzero
ID: 19590099
Thats excellent :-)
Thanks very much for your extra time!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question