GingerRobot Posted July 7, 2006 Share Posted July 7, 2006 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? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 7, 2006 Share Posted July 7, 2006 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 Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 7, 2006 Author Share Posted July 7, 2006 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 Quote Link to comment Share on other sites More sharing options...
Crimpage Posted July 8, 2006 Share Posted July 8, 2006 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 8, 2006 Share Posted July 8, 2006 [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 * " Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.