Tobias Karl
Tobias Karl

Reputation: 55

Populate HTML Table with MySQL Table Data

I'm really new to HTML/PHP and I want to build a simple website that returns the data from my MySQL database in form of a table..

My code looks like this for now:

<?php
  $server = mysql_connect("server", "username", "password"); 
  $db = mysql_select_db("ni354077_2sql1", $server); 
  $query = mysql_query("SELECT * FROM Jobs"); 
?>

         <table>
            <tr>
                <td>AuftragsID</td>
                <td>Startort</td>
                <td>Zielort</td>
                <td>Gewicht</td>
                <td>Fracht</td>
            </tr>

            <?php
               while ($row = mysql_fetch_array($query)) {
                   echo "<tr>";
                   echo "<td>".$row[AuftragsID]."</td>";
                   echo "<td>".$row[Startort]."</td>";
                   echo "<td>".$row[Zielort]."</td>";
                   echo "<td>".$row[Gewicht]."</td>";
                   echo "<td>".$row[Fracht]."</td>";
                   echo "</tr>";
               }

            ?>
        </table>

However it doesn't seem to be working properly. My table is full of "echo (..)" here is a Picutre of what it looks like: https://i.sstatic.net/6eGCg.png

Am I missing something ? I'm coming from C# WinForms and am confused with that.

Upvotes: 5

Views: 27079

Answers (5)

Scoffy
Scoffy

Reputation: 1

first use table row then use the loop inside PHP then populate your table with the data from the database

    <tr>
   <?php
     while ($row = mysql_fetch_array($query)) 
     {
   ?>
   <td><?php echo $row["AuftragsID"]?></td>
   <td><?php echo $row["Startort"] ?></td>
   <td><?php echo $row[Zielort]?></td>
   <td><?php echo $row[Gewicht]?></td>
   <td> <?php echo $row[Fracht]?></td>
</tr>
<?php }?>

Upvotes: 0

sakshi
sakshi

Reputation: 1

Try This Code:
<?php
  $server = mysql_connect("server", "username", "password"); 
  $db = mysql_select_db("ni354077_2sql1", $server); 
  $query = mysql_query("SELECT * FROM Jobs"); 
?>

         <table>
            <tr>
                <td>AuftragsID</td>
                <td>Startort</td>
                <td>Zielort</td>
                <td>Gewicht</td>
                <td>Fracht</td>
            </tr>

            <?php
               while ($row = mysql_fetch_array($query)) {
?>
                  <tr>
                <td><?php echo $row[AuftragsID]; ?></td>
                <td><?php echo $row[Startort]; ?></td>
                <td><?php echo $row[Zielort]; ?></td>
                <td><?php echo $row[Gewicht]; ?></td>
                <td><?php echo $row[Fracht]; ?></td>
            </tr>
    <?php
               }

            ?>
        </table>

Upvotes: 0

devnull
devnull

Reputation: 1928

First of all, you mentioned that you're new to PHP.

According to PHP mYSQL Documentation.

Warning This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

In other words. mySql will not be available and you better start using mySqli or PDO (PHP DATA OBJECTS).

MYSQLI: http://php.net/manual/en/book.mysqli.php

PDO: http://php.net/manual/en/book.pdo.php

The error you're getting because you're getting the key of array without quotes

Right Syntax:

$arrName['keyName'];

So you're solution will be like this

<?php
while ($row = mysql_fetch_array($query)) {
    echo "<tr>";
    echo "<td>" . $row['AuftragsID'] . "</td>";
    echo "<td>" . $row['Startort'] . "</td>";
    echo "<td>" . $row['Zielort'] . "</td>";
    echo "<td>" . $row['Gewicht'] . "</td>";
    echo "<td>" . $row['Fracht'] . "</td>";
    echo "</tr>";
}

?>

Upvotes: 0

Vivek Singh
Vivek Singh

Reputation: 2447

use this u have missed single quotes around php values, put like below

          <?php
               while ($row = mysql_fetch_array($query)) {?>
                   <tr>
                   <td><?php echo $row['AuftragsID'];?></td>
                   <td><?php echo $row['Startort'];?></td>
                   <td><?php echo $row['Zielort'];?></td>
                   <td><?php echo $row['Gewicht'];?></td>
                   <td><?php echo $row['Fracht'];?></td>
                   </tr>
              <?php  } ?>

Upvotes: 5

WebDeveloper
WebDeveloper

Reputation: 1

change the loop to

 while ($row = mysql_fetch_array($query)) {

               echo "<tr>";
               echo "<td>".$row['AuftragsID']."</td>";
               echo "<td>".$row['Startort']."</td>";
               echo "<td>".$row['Zielort']."</td>";
               echo "<td>".$row['Gewicht']."</td>";
               echo "<td>".$row['Fracht']."</td>";
               echo "</tr>";
           }

Upvotes: -1

Related Questions