cgwebprojects
cgwebprojects

Reputation: 3472

Select next column to right mysql?

If I have a variable in php like,

$var = 'aaa';

And my mysql database has 2 columns

| aaa | bbb |

Is there a way I can select bbb using $var, basically select the column next to $var on the right?

Upvotes: 0

Views: 775

Answers (1)

praseodym
praseodym

Reputation: 2413

Use mysql_query("SHOW COLUMNS FROM {table}",$db) to get a list of columns and use that to figure out the name of the column next to aaa. You can then use that in further queries.

Implementing this results in something in the lines of (untested):

$columns = mysql_query("SHOW COLUMNS FROM {table}",$db) or die("mysql error"); 
if (mysql_num_rows($columns) > 0) {
    while ($row = mysql_fetch_assoc($columns)) {
        if ($prev == 'aaa') {
            $nextcol = $row['Field']; // 'bbb' in this case
            break;
        }
        $prev = $row['Field'];
    }
}

Upvotes: 1

Related Questions