Вопрос

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

Это было полезно?

Решение

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; 
}

Другие советы

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.

The docs are helpful. You can use mysql_fetch_assoc

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top