Page 1 of 1

Fetching array's

Posted: Mon Jul 03, 2006 1:54 pm
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

Posted: Mon Jul 03, 2006 1:59 pm
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.

Posted: Mon Jul 03, 2006 2:10 pm
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'];

Posted: Mon Jul 03, 2006 2:37 pm
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