Page 1 of 1

mysql_fetch_array into multi dimensional array

Posted: Thu Feb 05, 2009 3:48 pm
by rn14
I want to code below to work in such a way that I can access the five rows that have been returned from the database individually.

eg

Code: Select all

 
$row1 =  $data[0][0];
$row2 =  $data[0][2];
 

Code: Select all

 
$week1 = mysql_query("SELECT * FROM table where checkLive = $checklive and type = '' ORDER BY id DESC LIMIT 15,5;")
or die (mysql_error());
 
 
$data = array();
    
  while ($dbRow = mysql_fetch_assoc($week1))
  {
    $data[$dbRow['id']];
    
  }
 
 
 
 
Can anybody help I know this is basic but its causing me great problems.

Thanks in advance

Re: mysql_fetch_array into multi dimensional array

Posted: Thu Feb 05, 2009 3:51 pm
by mickeyunderscore

Code: Select all

$data = array();
 
while ($dbRow = mysql_fetch_assoc($week1)){
      $data[] = $dbRow;   
}
This will give you the result you want.

Re: mysql_fetch_array into multi dimensional array

Posted: Thu Feb 05, 2009 4:03 pm
by rn14
Thanks, but then if i add this outside the loop no result is returned

Code: Select all

 
$start = $data[0][0];
echo $start;
 
 
I need to be able to access the five results returned individually

Re: mysql_fetch_array into multi dimensional array

Posted: Thu Feb 05, 2009 4:11 pm
by mickeyunderscore
Replace mysql_fetch_assoc with mysql_fetch_array.

Re: mysql_fetch_array into multi dimensional array

Posted: Thu Feb 05, 2009 4:17 pm
by rn14
worked a treat thanks for that

Re: mysql_fetch_array into multi dimensional array

Posted: Thu Feb 05, 2009 5:10 pm
by RobertGonzalez
If you will only need numeric indices in your second dimension consider using mysql_fetch_row() instead of mysql_fetch_array(). *_array() returns an array that contains both the associative and numeric indices so that is actually doubling the size of your result array. If you know you will only need associative indices, use *_assoc(), for numerics use *_row(). For both, of course use *_array().