Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Whats different between mysql_fetch_array and mysql_fetch_assoc?

I did a testing from my database.

$a echo out first row twice, I use count() to check and it print out double from my column $b echo out second row once.

Anyone can tell me why?

$connect=mysql_connect("localhost", "root", "");
$database=mysql_select_db("phplogin", $connect);

$SQL=mysql_query("SELECT * FROM users");

$a=implode(mysql_fetch_array($SQL));
$b=implode(mysql_fetch_assoc($SQL));


echo $a . "<HR/>";
echo $b;
share|improve this question

closed as not constructive by j0k, Krister Andersson, PaulStock, Michael Berkowski, Wouter J Dec 14 '12 at 15:08

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

3  
Start by reading the manual: php.net/mysql_fetch_assoc, php.net/mysql_fetch_array –  deceze Dec 14 '12 at 12:48
1  
you can get complete explanation just if you google it..show some research effort before posting a question here –  Chella Dec 14 '12 at 13:19
    
possible duplicate of mysql_fetch_array, mysql_fetch_assoc, mysql_fetch_object –  Michael Berkowski Dec 14 '12 at 14:56

1 Answer 1

  1. mysql_fetch_assoc — Fetch a result row as an associative array
  2. mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both

mysql_fetch_array() returns essentially two arrays, one with a numeric index and one with an associative based key index. Thus using mysql_fetch_array() without specifying which method you want (either MYSQL_NUM or MYSQL_ASSOC) always returns a double array, which is considerably more inefficient as compared to mysql_fetch_row() or mysql_fetch_assoc().

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.