By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,253 Members | 1,701 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,253 IT Pros & Developers. It's quick & easy.

Content within a while loop isn't echoing

P: 2
As a PHP learning exercise, I'm trying to program a "star trek" game. The game allows a list of commands, which are executed through a switch statement. The command below (case 8) is to 'scan an enemy vessel'. It should pull enemy vessel info from a database and display it in a table. The table border is all that appears...nothing is inside the border (which is collapsed to about 1px in height).

Expand|Select|Wrap|Line Numbers
  1. case "8":
  2.             global $enemyType;
  3.             if ($con)
  4.             {    echo "Initiating scan, Captain.";
  5.                 echo "<br /><table class='tableData' width='50%' cellpadding = '2'><tbody>";
  6.                 $query = "SELECT * FROM rom_warbird";
  7.                 $result = mysql_query($query) or die("Unable to retrieve scan data Sir");
  8.                 while($row=mysql_fetch_array($result, MYSQL_ASSOC))
  9.                 {
  10.                     $es1 = $row['0'];
  11.                     $es2 = $row['1'];
  12.                     $es3 = $row['2'];
  13.                     $es4 = $row['3'];
  14.                     $ews1 = $row['4'];
  15.                     $ews1Cond = $row['ews1Cond'];
  16.                     $ews1_stat = $row['ews1_stat'];
  17.                     $ee1 = $row['ee1'];
  18.                     $e_crew = $row['e_crew'];
  20.                     echo "<tr><td>$enemyType</td></tr>";
  21.                     echo "<tr><td><hr></td></tr>";
  22.                     echo "<tr><td>Shields at: " . $es1 . ", " . $es2 . ", " . $es3 . ", " . $es4 . "</td></tr>";
  23.                     echo "<tr><td>Weapon Systems include " . $ews1 . " , at " . $ews1Cond . " and " . $ews1_stat . " . </td></tr>";
  24.                     echo "<tr><td>Engines at " . $ee1 . " percent. </td></tr>";
  25.                     echo "<tr><td>She has a crew of " . $e_crew . " remaining.</td></tr>";
  26.                 }
  27.                 echo "</tbody></table>";
  28.             }            
  30.             break;
The Firefox SGML parser reports "Error: end tag for "tbody" which is not finished" on the line of code where tbody first appears. According to the parser there are no table rows. In fact, nothing that I try to echo from within that while loop will actually echo.

What am I doing wrong?
Apr 30 '10 #1
Share this Question
Share on Google+
8 Replies

Expert 100+
P: 1,584
mysql_fetch_arr returns an array. Read the docs.

Do you have PHP error reporting turned on? You probably get a warning.

May 1 '10 #2

Expert 5K+
P: 5,058

Try removing the <tbody> elements from the table. They don't have to be there. - Unless you need them for styling purposes?

If that doesn't work, try just displaying the output as text. Go through it and see if it is as it should be.

Expand|Select|Wrap|Line Numbers
  1. // Instead of this:
  2. mysql_fetch_array($result, MYSQL_ASSOC);
  4. // You can do
  5. mysql_fetch_assoc($result);
May 1 '10 #3

P: 25
What I think is wrong is that on line 3 $con returns as false (or NULL) so the entire if clause is skipped. This would explain why none of the echos work and why the </tbody> end tag isn't recognized. I would recommend moving the ending tags outside of the if clause. Finally, add this to the end of your if clause:
Expand|Select|Wrap|Line Numbers
  1. // Instead of this
  2. if($con)
  3. {
  4. // Your Code
  5. }
  7. // Try This out
  8. if($con)
  9. {
  10. // Your Code
  11. } else
  12. {
  13. echo "There was an error";
  14. }
This way, if "There was an error" is returned, you know that it is because your if conditional is being skipped.

Here are some of my other suggestions that might further improve your script.
On line 8 try this out:
Expand|Select|Wrap|Line Numbers
  1. // Instead of this
  2. while($row=mysql_fetch_array($result, MYSQL_ASSOC))
  4. // Try this
  5. while($row=mysqli_fetch_array($result, MYSQLI_ASSOC))
  6. {
  7.     // What you want to do goes here.
  8. }
I would recommend updating to the latest version of PHP. Then you could use mysqli (mysql improved) functions instead of regular mysql. Finally, I always include an else section after every conditional to make sure that mysqli_fetch_array() actually returns a result. What could be your problem is everything in the conditional statement never actually runs because $row has no values.
May 2 '10 #4

P: 2
I'm loving these responses to my question. Thank you all!

Won't get to testing out your suggestions until Monday or Tuesday, but will post here again with results.
May 2 '10 #5

Expert 5K+
P: 5,058
I am pretty sure that you need to change the "while" to an "if".
That's not correct.
The idea is (presumably) to display all the rows in the table. The while loop will iterate over all the rows in the result set, assigning each row to the $row variable. - If you replace it with an if, you will only ever get the first row in the table.

Also, I would recommend updating to the latest version of PHP. Then you could use mysqli (mysql improved) functions instead of regular mysql.
Any version of PHP 5 will run the MySQLI extension... although I would also recommend updating, regardless xD
May 2 '10 #6

P: 25
I edited my post to reflect the correction. Thanks for pointing it out!
May 2 '10 #7

Expert 5K+
P: 5,058
No problem.
One additional thing. The while clause can not be followed by a else block. Only if clauses are followed by else clauses. - Loops, like the while clause, just run continually while the statement is true, at which point they terminate and the code continues. If the statement starts out false, they simply don't run at all.
May 2 '10 #8

P: 25
Changed that too. When I changed the if to a while I forgot about that. Thanks for pointing it out
May 2 '10 #9

Post your reply

Sign in to post your reply or Sign up for a free account.