0

There is a table called as 'myTable' which contains columns - MyFactor1, MyFactor2, and MyFactor3. I want to get those column names, but now it returns only 'MyFactor1'. I tried mysql_fetch_array/assoc/object, but they don't work. Could you have any ideas to resolve this? Thanks in advance. Here is my code:

<?php
$aaa = mysql_query("select column_name from `information_schema`.`columns` 
               where `table_schema` = 'myDB' and `table_name` in ('myTable')");
foreach ($bbb = mysql_fetch_row($aaa) as $taxokey => $taxovalue) {
?>
    <option value="<?php print($taxokey);?>"><?php print($taxovalue);?></option>
<?php
}
?>
user2499003
  • 151
  • 2
  • 3
  • 11

1 Answers1

2

mysql_fetch_row() returns an array for each row in the result set. Your initial state for foreach calls mysql_fetch_row() once -- it doesn't call mysql_fetch_row() for each row.

So your foreach is not looping over rows, it's looping over elements of the array returned for one row.

Here's a loop that works the way you intend:

while ($bbb = mysql_fetch_assoc($aaa)) {
        $taxokey = $bbb["column_name"];
        $taxovalue = $bbb["column_name"];
?>
    <option value="<?php print($taxokey);?>"><?php print($taxovalue);?></option>
<?php
}

I'm not sure what different strings you intended $taxokey and $taxovalue to contain. All you get from the query you ran is one string, name of each column.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828