excl209 Posted June 11, 2007 Share Posted June 11, 2007 Can someone take a look at my code and tell me why nothing shows in the table... and why the code at the bottom displays everything twice... ?!!?!?! How do I show individual variables from my database?1?! Right now I'm not trying to do anything except show my records from my database!!!! Many thanks in advance. Excel209 include 'db.php'; //This connects to my server & database $mysql = mysql_query("SELECT * FROM users"); $result = mysql_query($mysql); $numofrows = mysql_num_rows($result); echo "<TABLE BORDER=\"1\" width=\"24%\">\n"; echo "<TR bgcolor=\"lightblue\"> <TD>Username</TD> <TD>first</TD> <TD>last</TD></TR>\n"; for($i = 0; $i < $numofrows; $i++) { $row = mysql_fetch_array($result); if($i % 2) { echo "<TR bgcolor=\"#CCFFCC\">\n"; } else { echo "<TR bgcolor=\"#BFD8BC\">\n"; } echo "<TD>".$row['username']."</TD> <TD>".$row['first_name']."</TD> <TD>".$row['last_name']."</TD> \n"; echo "</TR>\n"; } echo "</TABLE>\n"; // This is me trying to get any type of result to show... $query = "SELECT * FROM users"; $result = mysql_query($query); while ($line = mysql_fetch_array($result)){ foreach ($line as $value){ echo $value; } } Link to comment https://forums.phpfreaks.com/topic/55102-solved-help-phpmysql-table-and-nothing-shows/ Share on other sites More sharing options...
paul2463 Posted June 11, 2007 Share Posted June 11, 2007 try something like this <?php include 'db.php'; //This connects to my server & database $mysql = mysql_query("SELECT * FROM users"); $result = mysql_query($mysql) or die ("Error in query" . mysql_error()); //always check for errors in the query just in case echo "<TABLE BORDER=\"1\" width=\"24%\">\n"; echo "<TR bgcolor=\"lightblue\"> <TD>Username</TD> <TD>first</TD> <TD>last</TD></TR>\n"; $count = 1; //start a different counter set to 1 while($row = mysql_fetch_array($result)) { if($count % 2) { //check to see if its directly divisible by 2 echo "<TR bgcolor=\"#CCFFCC\">\n"; } else { echo "<TR bgcolor=\"#BFD8BC\">\n"; } echo "<TD>".$row['username']."</TD> <TD>".$row['first_name']."</TD> <TD>".$row['last_name']."</TD> \n"; echo "</TR>\n"; $count++; //increment the counter } echo "</TABLE>\n"; // This is me trying to get any type of result to show... $query = "SELECT * FROM users"; $result = mysql_query($query) or die ("Error in query" . mysql_error()); //again check for any errors , if there was one above it will be repeated here too - same query while ($line = mysql_fetch_array($result)){ foreach ($line as $value){ echo $value; } } ?> Link to comment https://forums.phpfreaks.com/topic/55102-solved-help-phpmysql-table-and-nothing-shows/#findComment-272428 Share on other sites More sharing options...
excl209 Posted June 11, 2007 Author Share Posted June 11, 2007 If I use this code I get an error message... "Error in query".. "blah..blah...blah.." $mysql = mysql_query("SELECT * FROM users"); $result = mysql_query($mysql) or die ("Error in query" . mysql_error()); But if I use this code it works :- $query = "SELECT * FROM users"; $result = mysql_query($query) or die ("Error in query" . mysql_error()); Is this strange of what?! But thanks for the code... the table now works great!! Excl209. Link to comment https://forums.phpfreaks.com/topic/55102-solved-help-phpmysql-table-and-nothing-shows/#findComment-272650 Share on other sites More sharing options...
pocobueno1388 Posted June 11, 2007 Share Posted June 11, 2007 It's because in this code: <?php $mysql = mysql_query("SELECT * FROM users"); $result = mysql_query($mysql) or die ("Error in query" . mysql_error()); ?> You are putting two mysql_query()'s around one query. You would have to take the mysql_query out of the $mysql line, then it would work...but it would also be identical to the working code you posted above...so that is the only difference. Link to comment https://forums.phpfreaks.com/topic/55102-solved-help-phpmysql-table-and-nothing-shows/#findComment-272652 Share on other sites More sharing options...
excl209 Posted June 12, 2007 Author Share Posted June 12, 2007 Many thanks for the help... My code is now working 100%... and I actually understand why now.. Excl209 Link to comment https://forums.phpfreaks.com/topic/55102-solved-help-phpmysql-table-and-nothing-shows/#findComment-272878 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.