1

I am running a query with a while loop that creates an array of the column names

while($ContactLookup_Row = mysql_fetch_array($ContactLookup_Rs)) {
        $ContactLookup_Results[] = array(
        'sequence'=>$ContactLookup_Row["sequence"], 
        'forename'=>$ContactLookup_Row["forename"],
        'surname'=>$ContactLookup_Row["surname"]
         );
    }

How can I make the loop add all the column names as their own variables without having to type them all out manually?

Should I put $ContactsLookup_Results[]= array( before the while loop. And then ); after the while loop

And then I'm not sure about within the loop?

I want it to look like:

'ColumnName'=>$ContactLookup_Row["ColumnName"]

With a , on the end of each one but not the last one

  • Obligatory: [**don't use `mysql_` functions anymore**](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – DCoder Dec 31 '13 at 12:05
  • Can you post how the resulting array should then look like? Becouse I don't know what you mean... – Matthias Dunkel Dec 31 '13 at 12:07

2 Answers2

1

How about

while($ContactLookup_Row = mysql_fetch_array($ContactLookup_Rs)) {
        $ContactLookup_Results[] = $ContactLookup_Row;
}
invisal
  • 11,075
  • 4
  • 33
  • 54
0

Try this.

$sql="DESCRIBE $_table_name";

$result=mysql_query($sql) or die(mysql_error());

while($data = mysql_fetch_assoc($result)){

    $columns[$data['Field']]=$data['Field'];

}
Erick Briseño
  • 173
  • 1
  • 5