Page 1 of 1

List column name when value in a row equals......

Posted: Sat Sep 18, 2004 2:46 pm
by malakajoe
Hi,

I am trying to figure out how I can do this.

I want to return a column name when a value in a row = XXX. Here is an example.

I have an ID, so I can pull all data from the row in a select statement. I want to then find out the column name when any of 5 columns equals 1. Basically, these 5 columns can have 1 through 5 with no repeated values. Example results from query:

select * from testtable where id = '1';

Would return this

id = 1, testcolumn1 = 3, testcolumn2 = 2, testcolumn3 = 1, testcolumn4 = 5, and testcolumn5 = 4

I want to pull test3 column name.

Any help would be greatly appreciated.

THANKS!

Posted: Sat Sep 18, 2004 4:45 pm
by John Cartwright

Code: Select all

<?php


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

while ($row = mysql_fetch_array($result))
{
echo $row['testcolumn3'];
}

?>
I have no idea wut you meant, hopefully this is what you wanted

Posted: Sat Sep 18, 2004 6:08 pm
by feyd
here's a ~basic way to do what you want, I think:

Code: Select all

<?php

$lookfor = 'testcolumn';
$valuelookfor = 1;
while($row = mysql_fetch_assoc($query))
{
  reset($row);
  while(list($k,$v) = each($row))
    if($v == $valuelookfor && preg_match('#^' . preg_quote($lookfor, '#') . '#', $k))
      echo $k . "<br />\n";
}