gussy81 Posted May 29, 2011 Share Posted May 29, 2011 hi there all. hope i can get your help. i am creating a php page that talks to mysql tabes. i want to print query into my html page inside html tags. the first echo in my code works but the one embedded inside the html code prints nothing to the page. look at my code if it helps: <html><body> <?php mysql_connect("localhost", "root", "123456") or die("Connection Failed"); mysql_select_db("man_utd")or die("Connection Failed"); $name = $_POST['members']; $query = "select * from members where username = 'burns'"; $result = mysql_query($query); while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo $line['pass']; } ?> <table border="0" cellspacing="2" cellpadding="2"> <tr> <th><font face="Arial, Helvetica, sans-serif"><?php echo $line['pass']; ?></font></th> <th><font face="Arial, Helvetica, sans-serif">Value2</font></th> <th><font face="Arial, Helvetica, sans-serif">Value3</font></th> <th><font face="Arial, Helvetica, sans-serif">Value4</font></th> <th><font face="Arial, Helvetica, sans-serif">Value5</font></th> </tr> </body> </html> thanks all for any help Quote Link to comment https://forums.phpfreaks.com/topic/237795-php-echo-not-working-for-me/ Share on other sites More sharing options...
sunfighter Posted May 29, 2011 Share Posted May 29, 2011 Your missing a </table> tag, but it should work. Quote Link to comment https://forums.phpfreaks.com/topic/237795-php-echo-not-working-for-me/#findComment-1221958 Share on other sites More sharing options...
gussy81 Posted May 29, 2011 Author Share Posted May 29, 2011 nope still have same problem thanks for the spot on the table closing tag. its odd, as the first echo works fine. any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/237795-php-echo-not-working-for-me/#findComment-1221960 Share on other sites More sharing options...
Hybride Posted May 29, 2011 Share Posted May 29, 2011 The reason the second one isn't working is because it's not in your while loop. $line[pass] is declared only to exist within the while loop; you can't use it outside of it. Change your structure to something like: echo '<table border="0" cellspacing="2" cellpadding="2">'; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "<tr><td>".$line['pass']."</td></tr>"; } echo "</table>"; ?> That should work. Quote Link to comment https://forums.phpfreaks.com/topic/237795-php-echo-not-working-for-me/#findComment-1221961 Share on other sites More sharing options...
gussy81 Posted May 29, 2011 Author Share Posted May 29, 2011 i tried this and still no joy: <html> <body> <?php mysql_connect("localhost", "root", "bxiulide") or die("Connection Failed"); mysql_select_db("man_utd")or die("Connection Failed"); $name = $_POST['members']; $query = "select * from members where username = 'burns'"; $result = mysql_query($query); while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { //echo $line['username']; echo $line['pass']; } ?> <table border="0" cellspacing="2" cellpadding="2"> <tr> <th><font face="Arial, Helvetica, sans-serif"> <?php while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo $line['pass']; }?></font></th> <th><font face="Arial, Helvetica, sans-serif">Value23</font></th> <th><font face="Arial, Helvetica, sans-serif">Value3</font></th> <th><font face="Arial, Helvetica, sans-serif">Value4</font></th> <th><font face="Arial, Helvetica, sans-serif">Value5</font></th> </tr> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/237795-php-echo-not-working-for-me/#findComment-1221962 Share on other sites More sharing options...
xyph Posted May 29, 2011 Share Posted May 29, 2011 The reason the second one isn't working is because it's not in your while loop. $line[pass] is declared only to exist within the while loop; you can't use it outside of it. This is soooo incorrect. Check example to see otherwise <?php while( $a = 'string' ) { break; } echo $a; // echoes 'string' ?> Your problem is that you're using a while loop to define your array, and not just defining it. In english, here's this block of code while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { //echo $line['username']; echo $line['pass']; } Perform this action until it's not true - $line = mysql... etc Echo whatever contents of variable Go back to while loop. So, the while loop continues until mysql_fetch_array() returns FALSE. Therefor, you're defining $line as FALSE in order to end the loop, so when you try to echo it later, you're pretty much saying echo FALSE; What you want to do, when you want a single result as an array, is this. $line = mysql_fetch_array($result, MYSQL_ASSOC); Lose the while loop. Please understand the code before you start to code it. Copy+Pasting from tutorials will only get you so far. Take the time to LEARN what your script is doing and you'll find things start working. Quote Link to comment https://forums.phpfreaks.com/topic/237795-php-echo-not-working-for-me/#findComment-1221972 Share on other sites More sharing options...
gussy81 Posted May 29, 2011 Author Share Posted May 29, 2011 thank you my friend thAT is fantastic advice. that worked. Quote Link to comment https://forums.phpfreaks.com/topic/237795-php-echo-not-working-for-me/#findComment-1221974 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.