phppaper Posted January 7, 2010 Share Posted January 7, 2010 How can you display all of the records from MySQL table with PHP without knowing the field name?? Anyone can give you a sample code?? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/187516-display-data-from-mysql-with-php-without-knowing-the-field-name/ Share on other sites More sharing options...
trq Posted January 7, 2010 Share Posted January 7, 2010 Assuming $row holds your record. foreach ($row as $r) { echo $r; } Quote Link to comment https://forums.phpfreaks.com/topic/187516-display-data-from-mysql-with-php-without-knowing-the-field-name/#findComment-990088 Share on other sites More sharing options...
phppaper Posted January 7, 2010 Author Share Posted January 7, 2010 But how can you display each field as a cell of a table. (with one row table for one row of record)?? Quote Link to comment https://forums.phpfreaks.com/topic/187516-display-data-from-mysql-with-php-without-knowing-the-field-name/#findComment-990222 Share on other sites More sharing options...
trq Posted January 7, 2010 Share Posted January 7, 2010 As you echo each field add the required html. eg; while ($row = mysql_fetch_assoc($result)) { echo '<tr>'; foreach ($row as $r) { echo '<td>' . $r . '</td>'; } echo '</tr>'; } Quote Link to comment https://forums.phpfreaks.com/topic/187516-display-data-from-mysql-with-php-without-knowing-the-field-name/#findComment-990226 Share on other sites More sharing options...
ignace Posted January 7, 2010 Share Posted January 7, 2010 $query = 'SELECT * FROM users'; $result = mysql_query($query, $connection); $fieldCount = mysql_num_fields($result); for ($i = 0; $i < $fieldCount; ++$i) { echo mysql_field_name($result, $i), ', '; } Outputs: id, username, password, email_address Quote Link to comment https://forums.phpfreaks.com/topic/187516-display-data-from-mysql-with-php-without-knowing-the-field-name/#findComment-990231 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.