Populating HTML table with MySQL table data

I am really new to HTML / PHP and I want to create a simple website that returns data from my MySQL database as a table.

My code looks like this:

<?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 does not work correctly. My table is full of "echo (..)" here is the Picutre as it looks: http://i.imgur.com/xw9bWy5.png

Am I missing something? I come from C # WinForms and am confused about this.

+3


source to share


4 answers


use this u, skipped single quotes around php values ​​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  } ?>

      

+4


source


First of all, you mentioned that you are new to PHP.

According to PHP mYSQL documentation.

Warning This extension has been deprecated as of PHP 5.5.0 and will be removed in the future. The MySQLi or PDO_MySQL extension should be used instead.

In other words. mySql won't be available and you'd 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 are getting because you are getting an array key without quotes

Right syntax:

$arrName['keyName'];

      

So you will be the same solution as

<?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>";
}

?>

      

0


source


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>

      

0


source


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>";
           }

      

-1


source







All Articles