Fetching array's

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Fetching array's

Post by psurrena »

Could someone please explain the difference in using:

Code: Select all

mysql_fetch_array($result, MYSQL_ASSOC);
versus

Code: Select all

mysql_fetch_assoc($result);
Thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

If you are accessing the arrays via their associative keys then nothing. The fetch_array() function may also provide keys 0..N in addition to the column name keys. Check the PHP documentation -- I am sure it will tell you the difference.
(#10850)
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

So, when I retrieve an a few cells based on a single "$id", it's an associative array?
I'm having a bit of trouble grasping this concept.

Code: Select all

$id = $_GET['id'];
			
	$query   = "SELECT * FROM interest WHERE id='$id'";
	$result  = mysql_query($query) or die('Error : ' . mysql_error());  
	$row     = mysql_fetch_assoc($result); 
	
	$title    = $row['title'];
        $body = $row['body'];
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

it doesn't matter how many columns you're returning or how many rows or your search criteria. What matters is how you deal with the result resource (in your case you're using mysql_fetch_assoc()) which will always return an associative array.

I'd suggest you read up on the difference between an associative array and a numeric-key array.

The former uses strings as keys while the latter uses numbers (0 based) as keys.

ex:

Code: Select all

//assiciative array:
$names = array('name1'=>'bob','name2'=>'larry');
echo $names['name1'];
//yields bob

//numceric array (default):
$names = array('bob','larry');
echo $names[0];
//yields bob
Post Reply