-8

Iam trying to place data from SQL into array and simultaneously make the same array associated with name of source columns. This is function, what I got:

GetRowFromDb("`id`,`name`,`email`", " WHERE `id`='1'", "database1", "table1");

function GetRowFromDb ($column, $condition, $database, $table) { 
  $target="`$database`.`$table`";
  $query="SELECT $column FROM $target $condition";
  $indexedrow=mysql_fetch_row(mysql_query($query));
  return $indexedrow; 
}

This gives me back indexed array. When I want to know email, I need to use index (number) for example $result[2]. But I want to make my array associative with name of source columns, like this

print $result[email] //should return email

Thanks for reply! Marcel

3 Answers3

0

Use mysql_fetch_assoc instead of mysql_fetch_row.

GetRowFromDb("`id`,`name`,`email`", " WHERE `id`='1'", "database1", "table1");

function GetRowFromDb ($column, $condition, $database, $table) { 
  $target="`$database`.`$table`";
  $query="SELECT $column FROM $target $condition";
  $indexedrow=mysql_fetch_assoc(mysql_query($query));
  return $indexedrow; 
}
Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
0

you have to change this line

 $indexedrow=mysql_fetch_row(mysql_query($query));

to

 $indexedrow=mysql_fetch_assoc(mysql_query($query));

mysql_fetch_row returns array with numbers as keys and mysql_fetch_assoc returns the field names as keys.

mikaint
  • 343
  • 3
  • 12
0

The docs are helpful. You can use mysql_fetch_assoc

Madbreaks
  • 19,094
  • 7
  • 58
  • 72