nectar
nectar

Reputation: 9679

2D array in PHP

how can I store $result value in a 2d array. here my code-

$sql="SELECT a.userId, b.name, b.dob FROM tbltree a INNER JOIN tblprofile b ON a.userId = b.userId WHERE a.superId ='$uid'";
$result=mysql_query($sql,$link)or die(mysql_error());

2d array having three columns-userId | name | dob

Upvotes: 1

Views: 518

Answers (2)

RobertPitt
RobertPitt

Reputation: 57268

$sql = "SELECT a.userId, b.name, b.dob FROM tbltree a INNER JOIN tblprofile b ON a.userId = b.userId WHERE a.superId ='$uid' LIMIT 1";

Then we use mysql_fetch-assoc to collect a row

if(false != ($resource = mysql_query($sql)))
{
    $result = mysql_fetch_assoc($resource);
    // Dont use a while here as we only want to iterate 1 row.
}

echo $result['name'];

Also added "LIMIT 1" to your query

Upvotes: 0

Marc B
Marc B

Reputation: 360692

Something like this:

$sql = "..."
$result = mysql_query(...) ...;

$result_array = array();
while($row = mysql_fetch_assoc($result)) {
    $result_array[] = $row;
}

That will give you:

$result_array[0] = array('key1' => 'val1', 'key2' => 'val2', ...);
$result_array[1] = array('key1' => 'val1', 'key2' => 'val2', ...);
$result_array[2] = etc...

If you don't want an associate array for the sub-array, there's other fetch modes as well

Upvotes: 5

Related Questions