Page 1 of 1

getting column names from mysql

Posted: Tue Mar 16, 2004 4:07 pm
by dsdsdsdsd
hello;

<?php
$query = "select * from table where (column1='this_row')";
?>

ofcourse this will select the entire row;
however some cells within the row will be 'null';
for the non-null cells I want the NAMEs of the columns;

any thoughts?

thanks
Shannon Burnett
Asheville NC USA

Posted: Tue Mar 16, 2004 4:38 pm
by coreycollins
This will get you the names of the fields (columns).

http://www.php.net/manual/en/function.m ... fields.php

Posted: Tue Mar 16, 2004 4:56 pm
by infolock
why not say "IF($row['myfield'] == 'Null')" and define a variable with the column name.

Posted: Tue Mar 16, 2004 5:31 pm
by V
// get an array of the column names, however you do that, adodb here
// "show columns from table" maybe a helpful query otherwise
$columns = $conn->MetaColumnNames($table);

foreach ($colums as $col) {
$query[] = "ifnull($col, '$col')";
}

$sql = "select ".implode(',',$query)." from table where condition";

...

make sense? ifnull() returns the first argument if it's not null, else it returns the second.

Posted: Tue Mar 16, 2004 9:51 pm
by dsdsdsdsd
thanks for your responses

Posted: Wed Mar 17, 2004 4:23 am
by fastfingertips
To get the column name you have the mysql_fetch_field() that returns you an associative array or an object (i do not remember well at this time), to give you the code :

while($i<mysql_num_rows($result))
{
$colNames=mysql_fetch_field($result);
$i++;
}

Anyway to get the name of the field: $colNames->name (thuh it seems to be an object :lol: )