Jump to content

Recommended Posts

hi,

 

1. what is the difference between them? under what condition, should we use them correctly?

    mysql_fetch_array

    mysql_fetch_assoc

    mysql_fetch_result

 

2. i've 2 set of codes below, i think both has the same outcome & achieve the same objective. Do they?

 

Set (a)

$query = "SELECT * FROM $table WHERE id='$id'";

$result = mysql_query($query) or die("Query failed");

$count = mysql_num_row($result);

if($count == 1)

{ $error[] = 'id in use';

}

 

Set (b)

$query = "SELECT count(*) AS c FROM $table WHERE id='$id'";

$result = mysql_query($query) or die("Query failed");

$result_array = mysql_fetch_assoc($result);

if($result_array['c'] > 0)

{ $errors[] = 'id in use';

}

 

Link to comment
https://forums.phpfreaks.com/topic/114599-compare-mysql_fetch_assoc/
Share on other sites

Let's say you have a table called fruit with a couple of columns called id, name, color

 

Fruit

idnamecolor

0applered

1orangeorange

2bananayellow

3watermelongreen

4grapepurple

 

If we ran the following code:

 

$sql = "SELECT id, color, name FROM fruit";
$result = mysql_query($sql);

 

Using mysql_fetch_row will return a numeric array for each row of the table.  A numeric array is an array where each element's key is a number.  We would access each element with a number like so:

 

while ($list = mysql_fetch_row($result)) {
   echo "id: {$list[0]} <br />";
   echo "name: {$list[1]} <br />";
   echo "color: {$list[2]} <br />";
}

 

Using mysql_fetch_assoc will return an associative array for each row of the table.  An associative array is an array where each element's key is a word.  When retrieving an associative array from a db result source, the keys correspond to the column names returned from the query.  So, we would access each element with the column name like this:

 

while ($list = mysql_fetch_assoc($result)) {
   echo "id: {$list['id']} <br />";
   echo "name: {$list['name']} <br />";
   echo "color: {$list['color']} <br />";
}

 

mysql_fetch_array returns both a numeric and associative array.  If you were to put mysql_fetch_array instead of _row or _assoc in either one of those loops above, it would produce the same result.  mysql_fetch_array will take an optional 2nd argument.  The default is MYSQL_BOTH which returns both numerical and associative.  That's what's returned by default if you don't specify the 2nd argument.  But you can specify for it to only return one or the other, using MYSQL_ASSOC for associative or MYSQL_NUM for numerical.  Yes, specifying for only 1 or the other makes it the exact same as just using _assoc or _row.  Overkill.

 

There's no such thing as mysql_fetch_result.  There is however, mysql_result.  Go back up and look at the table with the data.  Let's do the same query as before:

 

$sql = "SELECT id, color, name FROM fruit";
$result = mysql_query($sql);

 

This will return all the data from all of the rows. With mysql_result you can retrieve the data from the specific row and column like with x,y coordinates on a grid.  So let's say we wanted to print "green" from that table, we would do this:

 

echo mysql_result($result, 3, 2);

 

The first argument is the result source.  The second argument is which row. "green" is in the 4th row, but rows start at 0 so it's on row 3.  "green" is in the 3rd column, but numerical arrays start at 0 too, so it's column 2.  mysql_result will take either a numerical or associative key as the 3rd argument, so we could have done "color" instead of 2. 

 

As far as "when to use" which one... There's really very little circumstances where you *need* to have both an associative and numeric array returned, so mysql_fetch_array should rarely be used.  As far as whether to use mysql_fetch_assoc vs. mysql_fetch_row ... well, it depends on the situation.  But overall, it's easier to read and understand what's going on in your code if you use _assoc wherever possible. 

 

As far as mysql_result goes... to be honest, I have yet to be in a situation where this is actually useful.  One of the comments in the manual mentions utilizing it to "look ahead" to the next row's data while looping through rows, which I suppose is useful, but whatever. 

 

So yeah... that's about it.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.