Content within a while loop isn't echoing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Runtheball
    New Member
    • Apr 2010
    • 2

    #1

    Content within a while loop isn't echoing

    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...nothi ng is inside the border (which is collapsed to about 1px in height).

    Code:
    case "8":
    		    global $enemyType;
    			if ($con)
    			{	echo "Initiating scan, Captain.";
    				echo "<br /><table class='tableData' width='50%' cellpadding = '2'><tbody>";
    				$query = "SELECT * FROM rom_warbird";
    				$result = mysql_query($query) or die("Unable to retrieve scan data Sir");
    				while($row=mysql_fetch_array($result, MYSQL_ASSOC))
    				{
    					$es1 = $row['0'];
    					$es2 = $row['1'];
    					$es3 = $row['2'];
    					$es4 = $row['3'];
    					$ews1 = $row['4'];
    					$ews1Cond = $row['ews1Cond'];
    					$ews1_stat = $row['ews1_stat'];
    					$ee1 = $row['ee1'];
    					$e_crew = $row['e_crew'];
    					
    					echo "<tr><td>$enemyType</td></tr>";
    					echo "<tr><td><hr></td></tr>";
    					echo "<tr><td>Shields at: " . $es1 . ", " . $es2 . ", " . $es3 . ", " . $es4 . "</td></tr>";
    					echo "<tr><td>Weapon Systems include " . $ews1 . " , at " . $ews1Cond . " and " . $ews1_stat . " . </td></tr>";
    					echo "<tr><td>Engines at " . $ee1 . " percent. </td></tr>";
    					echo "<tr><td>She has a crew of " . $e_crew . " remaining.</td></tr>";
    				}
    				echo "</tbody></table>";
    			}			
    					
    			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?
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    mysql_fetch_arr returns an array. Read the docs.

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


    Dan

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hey.

      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.

      P.S.
      [code=php]// Instead of this:
      mysql_fetch_arr ay($result, MYSQL_ASSOC);

      // You can do
      mysql_fetch_ass oc($result);[/code]

      Comment

      • dgourd
        New Member
        • Apr 2010
        • 25

        #4
        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:
        Code:
        // Instead of this
        if($con)
        {
        // Your Code
        }
        
        // Try This out
        if($con)
        {
        // Your Code
        } else
        {
        echo "There was an error";
        }
        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:
        Code:
        // Instead of this
        while($row=mysql_fetch_array($result, MYSQL_ASSOC))
        
        // Try this
        while($row=mysqli_fetch_array($result, MYSQLI_ASSOC))
        {
            // What you want to do goes here.
        }
        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_ar ray() actually returns a result. What could be your problem is everything in the conditional statement never actually runs because $row has no values.

        Comment

        • Runtheball
          New Member
          • Apr 2010
          • 2

          #5
          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.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            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

            Comment

            • dgourd
              New Member
              • Apr 2010
              • 25

              #7
              Originally posted by Atli
              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.


              Any version of PHP 5 will run the MySQLI extension... although I would also recommend updating, regardless xD
              I edited my post to reflect the correction. Thanks for pointing it out!

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                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.

                Comment

                • dgourd
                  New Member
                  • Apr 2010
                  • 25

                  #9
                  Originally posted by Atli
                  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.
                  Changed that too. When I changed the if to a while I forgot about that. Thanks for pointing it out

                  Comment

                  Working...