1

Im not a hardvore coder. Im learning php/htmp/mysql/css now :)

So, i make mysql query with php, and now i want display the query result in a html table. But i don't know, how can i transfer variables between php and html code.

My php file now look like this:

<?php

$con=mysqli_connect("my_db_host","my_db_username","my_db_pass","my_db_name");

if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM hlstats_Servers
WHERE serverId='2'");

while($row = mysqli_fetch_array($result))
{
$name=$row['name'];
$ip=$row['address'] . ":" . $row['port'];
$player=$row['act_players'] . "/" . $row[max_players];
$map=$row['act_map'];
}    

mysqli_close($con);
?>

<html>
<head>
</head>
    <body>
        <table>
              <tr>
                  <td>$name</td>
              </tr>
        </table>

        <table>
              <tr>
                  <td>Ip cím:</td>
                  <td>$ip</td>
              </tr>
              <tr> 
                  <td>Jelenlegi pálya:</td>
                  <td>$map</td>
              </tr>
              <tr>
                  <td>Játékosok</td>
                  <td>$player</td>
              </tr> 
          </table>
      </body>
</html>
1
  • Instead of just placing the variable name (e.g. $name) use <?php echo $name; ?> Commented Dec 1, 2013 at 1:58

2 Answers 2

1

Sok sikert (good luck) :)

<?php
    $con = mysqli_connect("my_db_host", "my_db_username", "my_db_pass", "my_db_name");

    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query(
        $con,
        "SELECT * FROM hlstats_Servers
        WHERE serverId='2'"
    );
?>

<html>
    <head></head>
    <body>
        <table>
            <?php while ($row = mysqli_fetch_array($result)): ?>
                <?php $name = $row['name']; ?>
                <?php $ip = $row['address'] . ":" . $row['port']; ?>
                <?php $player = $row['act_players'] . "/" . $row['max_players']; ?>
                <?php $map = $row['act_map']; ?>

                <tr>
                    <td colspan="2"><?php echo $name; ?></td>
                </tr>
                <tr>
                    <td>Ip cím:</td>
                    <td><?php echo $ip; ?></td>
                </tr>
                <tr>
                    <td>Jelenlegi pálya:</td>
                    <td><?php echo $map; ?></td>
                </tr>
                <tr>
                    <td>Játékosok</td>
                    <td><?php echo $player; ?></td>
                </tr>
            <?php endwhile; ?>
        </table>
    </body>
</html>

<?php mysqli_close($con); ?>
Sign up to request clarification or add additional context in comments.

Comments

0

You cannot "transfer" variables from the PHP code to the HTML code, because the latter is not a programming language, it is only a structured markup language describing a webpage without variables or procedures.

Instead, you can show the current value of a PHP variable, and then the HTML markup will contain the actual value of that variable. For example:

<table>
      <tr>
          <td><?php echo $name; ?></td>
      </tr>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.