peuge Posted March 26, 2008 Share Posted March 26, 2008 Hi, I am selecting everything from a table and now I want to display it all. The only problem is that each thing comes as an element of the array $row. If I echo $row[1], $row[2] then I get what I want but is there an easier way? Am I using the wrong fetch command? Thanks. $result = mysql_query("SELECT * FROM $tables[0]"); while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row[0] . "</td>"; echo "</tr>"; } Link to comment https://forums.phpfreaks.com/topic/97968-selecting-from-tables/ Share on other sites More sharing options...
paul2463 Posted March 26, 2008 Share Posted March 26, 2008 if you change it to mysql_fetch_assoc($result); then you will be able to use the column names such as $first = $row['firstname']; $last = $row['lastname']; Link to comment https://forums.phpfreaks.com/topic/97968-selecting-from-tables/#findComment-501247 Share on other sites More sharing options...
peuge Posted March 26, 2008 Author Share Posted March 26, 2008 Sorry I was not quite clear in my first post. I am creating something along the lines of the user selects a database of his/her choice and then it displays the data for that database. Therefore using 'firstname' might apply to a table in one database but not the other. So is there another way? Thanks Link to comment https://forums.phpfreaks.com/topic/97968-selecting-from-tables/#findComment-501251 Share on other sites More sharing options...
paul2463 Posted March 26, 2008 Share Posted March 26, 2008 the examples i put are just that, examples, change the words inside the brackets to what column names you have in your database this makes it easier for me because i know what column names I am pulling from the database so the I can use the column names and not the array key makes my life more interesting Link to comment https://forums.phpfreaks.com/topic/97968-selecting-from-tables/#findComment-501253 Share on other sites More sharing options...
trq Posted March 26, 2008 Share Posted March 26, 2008 while($row = mysql_fetch_array($result)) { foreach ($row as $r) { echo "<tr>"; echo "<td>" . $r . "</td>"; echo "</tr>"; } } Link to comment https://forums.phpfreaks.com/topic/97968-selecting-from-tables/#findComment-501254 Share on other sites More sharing options...
peuge Posted March 26, 2008 Author Share Posted March 26, 2008 Paul2463: Thanks. But the code still does not help because I have different tables in different databases with different heading names as well as the tables have a different number of fields. Thorpe: It seems to be giving me duplicates as well as not putting them next to each other in the table? Sorry im quite new to this. Thanks Link to comment https://forums.phpfreaks.com/topic/97968-selecting-from-tables/#findComment-501261 Share on other sites More sharing options...
peuge Posted March 26, 2008 Author Share Posted March 26, 2008 This is my output: 11 is the ID pp is the username and c483f6ce851c9ecd9fb835ff7551737c is the md5 password. id username password 11 11 pp pp c483f6ce851c9ecd9fb835ff7551737c c483f6ce851c9ecd9fb835ff7551737c Link to comment https://forums.phpfreaks.com/topic/97968-selecting-from-tables/#findComment-501264 Share on other sites More sharing options...
trq Posted March 26, 2008 Share Posted March 26, 2008 Yeah, sorry. while($row = mysql_fetch_array($result,MYSQL_NUM)) { echo "<tr>"; foreach ($row as $r) { echo "<td>" . $r . "</td>"; } echo "</tr>"; } Link to comment https://forums.phpfreaks.com/topic/97968-selecting-from-tables/#findComment-501279 Share on other sites More sharing options...
peuge Posted March 26, 2008 Author Share Posted March 26, 2008 cool, thanks man, works perfectly! Link to comment https://forums.phpfreaks.com/topic/97968-selecting-from-tables/#findComment-501304 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.