mjsulliv Posted December 8, 2010 Share Posted December 8, 2010 I have an app that is the interface to a MySQL db. Based on inputs, it builds a table and selects what to return. The resultant array is parsed into row-arrays using “mysql_fetch_array()”. I know the returned row-array can be indexed by either numerical or associative strings; the associative strings representing the column values of the table(s) queried. What I’d like to do is use the associative index string values from the row array for the <th> value of the table being built. Is there a way to get access to these strings?Any help will be appreciated. --- Mike Link to comment Share on other sites More sharing options...
iwato Posted December 8, 2010 Share Posted December 8, 2010 What I’d like to do is use the associative index string values from the row array for the <th> value of the table being built. Is there a way to get access to these strings?I can think of several ways to achieve your goal, but this is probably the simplest and most direct -- the array_keys() function in conjunction with a foreach-statement.Roddy Link to comment Share on other sites More sharing options...
boen_robot Posted December 8, 2010 Share Posted December 8, 2010 An even easier way is probably to use the extended form of foreach, like: foreach($row as $columnName => $columnValue) [edit]Oh wait... you want to have the column names in a header... I suppose using array_keys() to first form the headers, and then using the above on the actual values is indeed a better solution.[/edit] Link to comment Share on other sites More sharing options...
mjsulliv Posted December 8, 2010 Author Share Posted December 8, 2010 Thank you both for the quick answer. Just what I needed. Link to comment Share on other sites More sharing options...
boen_robot Posted December 8, 2010 Share Posted December 8, 2010 BTW, while the above approach is the easy way, if you want your app to also support overlapping column names (SQL may return two columns with the same name, but PHP will only show the last one with that name if you use fetch_assoc()), you can use an indexed array in combination with mysqli_result::fetch_fields() to get the name (and more) of the indexed fields. Link to comment Share on other sites More sharing options...
Fmdpa Posted December 8, 2010 Share Posted December 8, 2010 Although this function isn't specifically for MySQL, you might consider looking at it anyway. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.