Emil Dumbazu
Emil Dumbazu

Reputation: 662

get column name in a query

How could i get the column name of an empty record from a table ? For example:

SELECT column.name FROM table WHERE id='1111' AND column_name=''

It should return every title of an empty column from table.

Upvotes: 0

Views: 448

Answers (3)

user3129207
user3129207

Reputation: 61

This is also one method:

$mysql_query = mysql_query("describe table");
while ($row = mysql_fetch_array($mysql_query)) {
 echo $row[0]."</br>";
}

Upvotes: 1

worenga
worenga

Reputation: 5856

i would do it with php for simplicity's sake, however there might be a sophisticated mysql approach to do this at dbms level

in PseudoCode:

$result = MYSQL_FETCH_ASSOC ( MYSQL_QUERY( SELECT * FROM table WHERE id='1111' ) )

$blankCols = array();
foreach($result[0] as $key=>$value){
   if($value=="")
   {
      $blankCols[] = $key;
   }
}

as i mentioned before, this seems ugly and looks like a bad data structure design.

Upvotes: 0

Nesim Razon
Nesim Razon

Reputation: 9794

If you using mysql 5.0+ you can get a table's all column names like this:

SELECT COLUMN_NAME
FROM `information_schema`.`COLUMNS`
WHERE TABLE_NAME='$table_name'
ORDER BY ORDINAL_POSITION

Note: Chane $table_name to the name of your table's name.

Upvotes: 1

Related Questions