getting column names from mysql

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
dsdsdsdsd
Forum Commoner
Posts: 60
Joined: Fri Dec 05, 2003 3:10 pm

getting column names from mysql

Post 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
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

This will get you the names of the fields (columns).

http://www.php.net/manual/en/function.m ... fields.php
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

why not say "IF($row['myfield'] == 'Null')" and define a variable with the column name.
V
Forum Newbie
Posts: 3
Joined: Tue Mar 16, 2004 1:15 pm
Location: Keller, Tx.
Contact:

Post 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.
dsdsdsdsd
Forum Commoner
Posts: 60
Joined: Fri Dec 05, 2003 3:10 pm

Post by dsdsdsdsd »

thanks for your responses
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post 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: )
Post Reply