mysql_fetch_array into multi dimensional array

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
rn14
Forum Newbie
Posts: 8
Joined: Sun Oct 12, 2008 11:16 am

mysql_fetch_array into multi dimensional array

Post 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
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: mysql_fetch_array into multi dimensional array

Post by mickeyunderscore »

Code: Select all

$data = array();
 
while ($dbRow = mysql_fetch_assoc($week1)){
      $data[] = $dbRow;   
}
This will give you the result you want.
rn14
Forum Newbie
Posts: 8
Joined: Sun Oct 12, 2008 11:16 am

Re: mysql_fetch_array into multi dimensional array

Post 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
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: mysql_fetch_array into multi dimensional array

Post by mickeyunderscore »

Replace mysql_fetch_assoc with mysql_fetch_array.
rn14
Forum Newbie
Posts: 8
Joined: Sun Oct 12, 2008 11:16 am

Re: mysql_fetch_array into multi dimensional array

Post by rn14 »

worked a treat thanks for that
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: mysql_fetch_array into multi dimensional array

Post 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().
Post Reply