0

Currently i am doing a PHP GET and getting values after clicking the submit button.

I got the values out from the submit button but I dont know how to reference them to the values in column.

For example, after clicking the submit button, I get the value 1.

Inside my database i have 3 columns

Col 1 Col 2 Col 3

 2   3  1

How do i get the COLUMN name by just having the value itself? Thank you so much!

Newbie
  • 145
  • 2
  • 7
  • 23
  • mysqli_result -> fetch fields (php.net link) -> http://php.net/manual/en/mysqli-result.fetch-fields.php – Marco Mura Nov 26 '14 at 14:17
  • you can't...because RDBMS Store and search data in row wise not column wise – Rajib Ghosh Nov 26 '14 at 14:18
  • fetch the row as an associative array (the keys will be the column names) then output the key based on the value – Jeff Hawthorne Nov 26 '14 at 14:23
  • at first you have to fetch all row and all column of our table after that you have to check your value with column value...then you get the column name otherwise you don't get :) – Rajib Ghosh Nov 26 '14 at 14:27

2 Answers2

1

Ugly/silly, but would do what you want:

Query:

SELECT * FROM yourtable WHERE 1 in (col1, col2, col3, ... ,colN)

and then manually pick out the value in client code:

$row = mysql_fetch_assoc($result);
$matches = array_search(1, $row);
var_dump($matches);

You can't get away from listing all of the columns in the query, because there's no WHERE x IN (*)-type construct.

Marc B
  • 356,200
  • 43
  • 426
  • 500
0

I would do it like this.

Get all table columns

then loop throug results and run select for each column

$columnVsValue = array();

foreach($columns as $column) {
  $q = "SELECT id FROM table where {$column} = {$value}";
  if(TRUE) { // you have to check that query return value
    $columnVsValue[] = $column;
  }
}

I did it quick probably there is more efficient way to do it

Community
  • 1
  • 1
Wojtek B
  • 160
  • 1
  • 12