5

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.

2
  • Looks like you have missed to close " somewhere before this code Commented Jun 6, 2015 at 7:12
  • you missed quotes around to the values, Please check my answer.thanks Commented Jun 6, 2015 at 7:17

5 Answers 5

5

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  } ?>
Sign up to request clarification or add additional context in comments.

3 Comments

Can you please explain what changes you have done to make it correct??
Semi-works now. My table looks fine now but it's empty. I've already checked for misspellings in my Database but that seems to be alright
@TobiasKarl so print your query and run in phpmyadmin and check results are coming or not and and use 'or die(mysql_error)'
0

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

?>

Comments

0
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>

Comments

0

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

1 Comment

Please format your code appropriately. It will be much easier to read and understand.
-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>";
           }

1 Comment

Add quotes to the field names

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.