Anony123
Anony123

Reputation: 55

mysql show all column names in array php

I have a mysql table with columns: Date, Jack, John, Amy. I want to show the column names (only the names!) in an array using php.

So the result should be an array like this:

$array = array('Date', 'Jack', 'John', 'Amy');

So when I do echo $array[1]; for example, the output should be 'Jack'.

Any help would be very appreciated.

Upvotes: 0

Views: 818

Answers (1)

Sunil Pachlangia
Sunil Pachlangia

Reputation: 2061

Use this code

$table_column = mysql_query("SHOW COLUMNS FROM YourTableName"); 
$tableArr = array();
while($row1 = mysql_fetch_assoc($table_column))
{
    $tableArr[] = $row1['Field'];
}

echo "<pre>";
print_r($tableArr);

Upvotes: 1

Related Questions