abdfahim Posted January 11, 2008 Share Posted January 11, 2008 Let consider the following code $query="SELECT * FROM table"; $result=mysql_query($query); Now we all know how to display the results. But consider I have only $result variable and I dont have any idea of the mysql table - how many field, field's name etc. - I know nothing. Even its not necessary that $query always contains all fields * (then I could use SHOW FIELDS command), may be some times it has selected fields like $query="SELECT (a,b,c,d,e) FROM table"; But I don't know which fields is included in Query. Then how can I display the Query Results with field names? Quote Link to comment https://forums.phpfreaks.com/topic/85475-solved-question-on-displaying-mysql_query-result-variable/ Share on other sites More sharing options...
trq Posted January 11, 2008 Share Posted January 11, 2008 <?php $query = "SELECT * FROM table"; if ($result = mysql_query($query)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { foreach ($row as $key => $value) { echo "$key = $value<br />"; } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/85475-solved-question-on-displaying-mysql_query-result-variable/#findComment-436179 Share on other sites More sharing options...
abdfahim Posted January 11, 2008 Author Share Posted January 11, 2008 Now this is cool. I didn't know that. thanx .... It shows each cell in a new line like field1 = abc field2 = def field3 = geh field1 = ijk field2 = lmn field3 = opq So if I have to show this in tabular form like field1 field2 field3 abc def geh ijk lmn opq should I need to use php functions (like explode to get field name and compare string to get when the next row start) or there is any straight forward proccess? Quote Link to comment https://forums.phpfreaks.com/topic/85475-solved-question-on-displaying-mysql_query-result-variable/#findComment-436186 Share on other sites More sharing options...
Ken2k7 Posted January 11, 2008 Share Posted January 11, 2008 Something like this? <?php $count = 0; echo "<table><tr><th>field1</th><th>field2</th><th>field3</th></tr>"; $query = "SELECT * FROM table"; if ($result = mysql_query($query)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { foreach ($row as $key => $value) { if($count!=0&&$count%3==0) echo "</tr><tr>"; echo "<td>$value</td>"; } } } } echo "</tr></table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/85475-solved-question-on-displaying-mysql_query-result-variable/#findComment-436196 Share on other sites More sharing options...
abdfahim Posted January 11, 2008 Author Share Posted January 11, 2008 nop, because I gave just an example, but actually, as I told, I don't know how many fields are there in the query and neither I know the fields' name . Quote Link to comment https://forums.phpfreaks.com/topic/85475-solved-question-on-displaying-mysql_query-result-variable/#findComment-436197 Share on other sites More sharing options...
trq Posted January 11, 2008 Share Posted January 11, 2008 Something like.... <?php $header = false; echo "<table>" $query = "SELECT * FROM table"; if ($result = mysql_query($query)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { if (!$header) { $fields = array_keys($row); echo " <tr>"; foreach($fields as $field) { echo " <td>$field</td>"; } echo " </tr>"; $header = true; } echo " <tr>"; foreach ($row as $value) { echo " <td>$value</td>"; } echo " </tr>"; } } } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/85475-solved-question-on-displaying-mysql_query-result-variable/#findComment-436204 Share on other sites More sharing options...
Ken2k7 Posted January 11, 2008 Share Posted January 11, 2008 thorpe, close out the table tag Quote Link to comment https://forums.phpfreaks.com/topic/85475-solved-question-on-displaying-mysql_query-result-variable/#findComment-436205 Share on other sites More sharing options...
abdfahim Posted January 11, 2008 Author Share Posted January 11, 2008 Now this is really cool. Thanx v v much. One thing, please modify ur code and put a semi-colon (";") at the end of 2nd line. Quote Link to comment https://forums.phpfreaks.com/topic/85475-solved-question-on-displaying-mysql_query-result-variable/#findComment-436214 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.