Jump to content


Photo

fetch_array or fetch_assoc?


  • Please log in to reply
4 replies to this topic

#1 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 07 July 2006 - 09:07 PM

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?

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 07 July 2006 - 09:30 PM

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

#3 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 07 July 2006 - 09:42 PM

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

#4 Crimpage

Crimpage
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts
  • LocationBrisbane, Australia

Posted 08 July 2006 - 12:19 AM

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.

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,025 posts

Posted 08 July 2006 - 08:24 AM

It's much easier to understand what someone's code is doing when they reference $row['userid'] vs $row[0].


How styles differ !

I prefer
<?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";
}
?>
Note: NOT "select * "
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users