Link to home
Create AccountLog in
Avatar of aej1973
aej1973

asked on

Print output of an associative array..

HI, I have a database as shown below:

person_id   name    age    year    gender
1                 Bob       30     1982   Male
2                 Ann       39     1973  Female


I  would like to run a query to get all the details of Bob and Ann as follows, how can this be done?


Array
(
[Bob] => Array
  (
  [0] => 30
  [1] => 1982
  [2] => Male
  )

[Ann] => Array
  (
  [0] => 39
  [1] =>1973
  [2] =>Female
  )
)

Thanks for the help.

A
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of aej1973
aej1973

ASKER

Ray, with the following code I am able to pick up only the first line..

<?php
include "connect.php";

$sql="select * from person where name ='bob'";
$result = mysql_query($sql);
$row=mysql_fetch_assoc($result);
$data = array();
foreach ($row as $data[]);
print_r($data);

My output is as follows:

Array ( [0] => 1 [1] =>Bob [2] => 30 [3] => 1982 [4] => Male )

I will need to print out all the lines, how do I do that?. Also, what do I need to do to print out only array[1] and array[4] from all the records? Thanks for the help.

A
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account