Jump to content

php echo not working for me


gussy81

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.