forumnz Posted September 24, 2007 Share Posted September 24, 2007 How does that work? Sya I have 10 users and want to display them all, how can I code something that places all 10 (say username and location) in rows as output. I know how to do the html part etc, and also how can I make it so that every odd number row has light grey and every even has darker? Thanks a lot, Sam. Quote Link to comment https://forums.phpfreaks.com/topic/70408-solved-display-multiple-db-rows/ Share on other sites More sharing options...
darkfreaks Posted September 24, 2007 Share Posted September 24, 2007 <?php echo"<table><tr><td> $row[username];$row[location]; </td></tr></table>";?> Quote Link to comment https://forums.phpfreaks.com/topic/70408-solved-display-multiple-db-rows/#findComment-353750 Share on other sites More sharing options...
forumnz Posted September 24, 2007 Author Share Posted September 24, 2007 So if I have 10 users and I select them all from bd then I just write the equiv. of that, it will/should work? Quote Link to comment https://forums.phpfreaks.com/topic/70408-solved-display-multiple-db-rows/#findComment-353751 Share on other sites More sharing options...
darkfreaks Posted September 24, 2007 Share Posted September 24, 2007 actually uou need $link= mysql connect or die mysql error $result = mysql_query("SELECT * FROM table", $link); $num_rows = mysql_num_rows($result); echo "<font face=Verdana size=1 color=484077> $num_rows </font>"; Quote Link to comment https://forums.phpfreaks.com/topic/70408-solved-display-multiple-db-rows/#findComment-353752 Share on other sites More sharing options...
trq Posted September 24, 2007 Share Posted September 24, 2007 Something like.... <?php // connect to db. $sql = "SELECT uname,location FROM users"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { $i = 1; echo "<table>"; while ($row = mysql_fetch_assoc($result)) { $color = ($i % 2) ? 'light' : 'dark'; echo " <tr class='$color'>"; echo " <td>{$row['uname']}</td>"; echo " <td>{$row['location']}</td>"; echo " </tr>"; $i++; } echo "</table>"; } else { echo "No results found"; } } else { echo "Query failed " . mysql_error() . "<br />$sql"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/70408-solved-display-multiple-db-rows/#findComment-353754 Share on other sites More sharing options...
forumnz Posted September 24, 2007 Author Share Posted September 24, 2007 Why does this not work (thanks thorpe)? It displays all of it on the same line. <?php //Connect to mysql server $link=mysql_connect("localhost","sss","sss"); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db=mysql_select_db("bbmembers"); if(!$db) { die("Unable to select database"); } else{ $sql = "SELECT * FROM auctions"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { $i = 1; echo "<table>"; while ($row = mysql_fetch_assoc($result)) { $color = ($i % 2) ? 'light' : 'dark'; echo " <td class='$color'>"; echo " <tr>{$row['uan']}</tr>"; echo " <tr>{$row['title']}</tr>"; echo " </td>"; $i++; } echo "</table>"; } else { echo "No results found"; } } else { echo "Query failed " . mysql_error() . "<br />$sql"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/70408-solved-display-multiple-db-rows/#findComment-353760 Share on other sites More sharing options...
darkfreaks Posted September 24, 2007 Share Posted September 24, 2007 name and location need to be in seperate td and tr's Quote Link to comment https://forums.phpfreaks.com/topic/70408-solved-display-multiple-db-rows/#findComment-353761 Share on other sites More sharing options...
forumnz Posted September 24, 2007 Author Share Posted September 24, 2007 But the next thing is after the</td>? Quote Link to comment https://forums.phpfreaks.com/topic/70408-solved-display-multiple-db-rows/#findComment-353762 Share on other sites More sharing options...
darkfreaks Posted September 24, 2007 Share Posted September 24, 2007 while ($row = mysql_fetch_assoc($result)) { $color = ($i % 2) ? 'light' : 'dark'; echo " <tr class='$color'>"; echo " <td>{$row['uname']}</td>"; echo " <td>{$row['location']}</td>"; echo " </tr>"; $i++; Quote Link to comment https://forums.phpfreaks.com/topic/70408-solved-display-multiple-db-rows/#findComment-353763 Share on other sites More sharing options...
forumnz Posted September 24, 2007 Author Share Posted September 24, 2007 Thanks, what did you do? Quote Link to comment https://forums.phpfreaks.com/topic/70408-solved-display-multiple-db-rows/#findComment-353764 Share on other sites More sharing options...
darkfreaks Posted September 24, 2007 Share Posted September 24, 2007 absolutely nothing? you copied his code wrong LOL Quote Link to comment https://forums.phpfreaks.com/topic/70408-solved-display-multiple-db-rows/#findComment-353765 Share on other sites More sharing options...
forumnz Posted September 24, 2007 Author Share Posted September 24, 2007 Oh I see... haha.... One other thing, how can I change the order ...ie asc or desc? I wrote ORDER BY asc in the select part but i got an error: Query failed You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc LIMIT 3' at line 1 SELECT * FROM auctions ORDER BY desc LIMIT 3 Quote Link to comment https://forums.phpfreaks.com/topic/70408-solved-display-multiple-db-rows/#findComment-353766 Share on other sites More sharing options...
darkfreaks Posted September 24, 2007 Share Posted September 24, 2007 DESC LIMIT 3 Quote Link to comment https://forums.phpfreaks.com/topic/70408-solved-display-multiple-db-rows/#findComment-353767 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.