gple Posted August 25, 2007 Share Posted August 25, 2007 How can I search through a table and output the name of each column? Quote Link to comment https://forums.phpfreaks.com/topic/66603-output-column-names/ Share on other sites More sharing options...
vijayfreaks Posted August 25, 2007 Share Posted August 25, 2007 Hi.. read the following code its from manual.. you can go 4 mysql function in it fro more info.. <?php // Connecting, selecting database $link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error()); echo 'Connected successfully'; mysql_select_db('my_database') or die('Could not select database'); // Performing SQL query $query = 'SELECT * FROM my_table'; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); // Printing results in HTML echo "<table>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n"; } echo "</table>\n"; // Free resultset mysql_free_result($result); // Closing connection mysql_close($link); ?> Regards, Vijay Quote Link to comment https://forums.phpfreaks.com/topic/66603-output-column-names/#findComment-333692 Share on other sites More sharing options...
Fadion Posted August 25, 2007 Share Posted August 25, 2007 Why do a foreach inside the while when $line and $col_value will print the exact thing. Anyway i guess the answer is to print column names and not row values, which i have no idea. Quote Link to comment https://forums.phpfreaks.com/topic/66603-output-column-names/#findComment-333694 Share on other sites More sharing options...
sasa Posted August 25, 2007 Share Posted August 25, 2007 try $result = mysql_query('DESCRIBE table_name'); while ($row = mysql_fetch_array($result)){ echo $field_name[] = $row['Field']; } i put names of fields in array in same time Quote Link to comment https://forums.phpfreaks.com/topic/66603-output-column-names/#findComment-333696 Share on other sites More sharing options...
teng84 Posted August 25, 2007 Share Posted August 25, 2007 also try: i think its simple look if you do some fetch array you will have this as value [field]=> value the field is there all you have to do is make a dynamic query like select * form $table limit 1 Quote Link to comment https://forums.phpfreaks.com/topic/66603-output-column-names/#findComment-333697 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.