php 使用 MySQL 表数据填充 HTML 表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30680011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:58:43  来源:igfitidea点击:

Populate HTML Table with MySQL Table Data

phpmysql

提问by Tobias Karl

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

我真的是 HTML/PHP 的新手,我想建立一个简单的网站,以表格的形式从我的 MySQL 数据库返回数据。

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: http://i.imgur.com/xw9bWy5.png

但是,它似乎无法正常工作。我的桌子上满是“回声(..)”,这是它的外观图片: http://i.imgur.com/xw9bWy5.png

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

我错过了什么吗?我来自 C# WinForms 并且对此感到困惑。

回答by Vivek Singh

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

使用这个你错过了 php 值周围的单引号,如下所示

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

回答by devnull

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

首先,您提到您是 PHP 新手。

According to PHP mYSQL Documentation.

根据 PHP mySQL 文档。

WarningThis 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.

警告此扩展自 PHP 5.5.0 起已弃用,将来会被删除。相反,应使用 MySQLi 或 PDO_MySQL 扩展。

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

换句话说。mySql 将不可用,您最好开始使用 mySqli 或 PDO(PHP 数据对象)。

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

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

PDO: http://php.net/manual/en/book.pdo.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>";
}

?>

回答by sakshi

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>

回答by Scoffy

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

首先使用表格行,然后使用 PHP 中的循环,然后使用数据库中的数据填充表格

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

回答by WebDeveloper

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