HTML似乎忽略了PHP命令?

I'm a complete newbie when it comes to HTML, I've never needed to do any webpage stuff before so apologies if I'm being a simpleton.

I have a high score table for an iOS app, and I want to display it on the website for the app as well. The leaderboard is working fine in the app using PHP and mySQL, but when I try to access the database using PHP inside an HTML file it doesn't seem to pay any attention to the logic of the PHP (i.e. if I have an if/else statement, it will run both even cases).

Here is a simplified version of my code, if connection to the database is successful then the first column in the table header will say "success", otherwise it will say "failure".

<body>

<table class="altrowstable" id="alternatecolor">

<?php
if (!mysql_connect($db_host, $db_user, $db_pwd))
{
    <tr>
    <th>Failure</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
    </tr>
}
else
{
    <tr>
    <th>Success</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
    </tr>
}

?>

</table>
</body>

Can anyone see what has been done incorrectly here? I assumed that the if/else would work as normal, but instead I get both rows inserted into the table, "failure" and "success".

Edit: I have also tried using echo ""; etc, but not sure which way is correct as they both give me the same error where it ignores the PHP.

Check extension of your file. It need to be filename.php You need a server to launch php code You can use echo heredoc like here

<body>

<table class="altrowstable" id="alternatecolor">

<?php
if (!mysql_connect($db_host, $db_user, $db_pwd))
{
echo <<<_END
<tr>
<th>Failure</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
_END
}
else
{
echo <<<_END
<tr>
<th>Success</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
_END
}
?>
</table>
</body>

That seems to be working

Change your code to

    <table class="altrowstable" id="alternatecolor">

    <?php
    if (!mysql_connect($db_host, $db_user, $db_pwd))
    {
?>
        <tr>
        <th>Failure</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
        </tr>
  <?php
   }
    else
    {
  ?>
        <tr>
        <th>Success</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
        </tr>
<?php
    }

    ?>

    </table>

Between <?php and ?> is just PHP code. It is treating you HTML as PHP.

Either

  1. Use echo
  2. put ?> before the HTML and <?php> after (i.e. stop and start PHP processing)

I think that you need to put your HTML tags in the echo command like this

echo '<tr>
      <th>Failure</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th>         <th>Draws</th><th>Losses</th>
      </tr>';

this should work

try this:

    <body>

    <table class="altrowstable" id="alternatecolor">

    <?php
    if (!mysql_connect($db_host, $db_user, $db_pwd))
    {
    ?>
        <tr>
        <th>Failure</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
        </tr>
<?php
    }
    else
    {
?>
        <tr>
        <th>Success</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
        </tr>
     <?php } ?>

    </table>
    </body>