php 将 SQL 数据放入 HTML 表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14548110/
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 07:29:50  来源:igfitidea点击:

Putting SQL data into HTML table

phphtmlsqlhtml-table

提问by Matt

I'm trying to get the data from my mySQL database and put them into a HTML table. After searching a lot on the internet, but I coudn't find code that worked for me.

我正在尝试从 mySQL 数据库中获取数据并将它们放入 HTML 表中。在互联网上搜索了很多之后,但我找不到对我有用的代码。

Currently, I have this code

目前,我有这个代码

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <table>
        <thead>
            <tr>
                <td>Naam</td>
                <td>Gemeente</td>
                <td>Datum</td>
            </tr>
        </thead>
        <tbody>
       <?php
          $db_select = mysql_select_db($dbname,$db);
            if (!db_select) {
                die("Database selection also failed miserably: " . mysql_error());
            }
            mysql_select_db("databaseiheko");
            $results = mysql_query("SELECT NaamFuif, GemeenteFuif, DatumFuif FROM tblfuiven");
            while($row = mysql_fetch_array($results)) {
            ?>
                <tr>
                    <td><?php echo $row['NaamFuif']?></td>
                    <td><?php echo $row['GemeenteFuif']?></td>
                    <td><?php echo &row['DatumFuif']?></td>
                </tr>

            <?php
            }
            ?>   
            </tbody>
            </table>
</body>
</html>

The only thing that I get is the first row of my table (Naam-Gemeente-Datum). Am I doing something wrong or did I forgot something?

我唯一得到的是表格的第一行(Naam-Gemeente-Datum)。是我做错了什么还是忘记了什么?

采纳答案by That Brazilian Guy

First of all, the most important thing to keep in mind is:

首先,要记住的最重要的事情是:

You are using deprecated and unsecure code

您正在使用已弃用且不安全的代码

The mysql_ functions are stronglydiscouraged, for various reasons:

由于各种原因,强烈建议不要使用mysql_ 函数

  • Are deprecated and will be removed in future versions of PHP,
  • Are insecure leading to possible SQL injections,
  • Lack many features present in more current versions of PHP
  • 已弃用,并将在 PHP 的未来版本中删除,
  • 不安全导致可能的 SQL 注入,
  • 缺少更多当前版本的 PHP 中存在的许多功能

See the linked question for muchmore in-depth explanations.

请参阅链接的问题很多更深入的解释。

Now, to the code itself:

现在,代码本身:

You are not using mysql_connectto connect to the server

您没有使用mysql_connect连接到服务器

You should use mysql_connectto specify the server, the username and the password that will be used to access the data in the database. From your code, it seems that it wassupposed to be present, because there's a $dbvariable used in the mysql_connectfunction, but not properly initialized nor used again anywhere else.

您应该使用mysql_connect来指定将用于访问数据库中数据的服务器、用户名和密码。从你的代码,它似乎应该存在,因为有一个$db在使用可变mysql_connect功能,但没有正确的初始化,也没有再使用其他任何地方。

You should use mysql_connectin a way similar to this:

您应该mysql_connect以类似于此的方式使用:

$db = mysql_connect('localhost', $user, '$password');
if (!$db) {
    die('Not connected : ' . mysql_error());
}

(Don't forget to set your username and password!)

(不要忘记设置您的用户名和密码!)

You are using mysql_select_dbtwice in a row:

mysql_select_db连续使用两次:

    $db_select = mysql_select_db($dbname,$db);
        if (!db_select) {
            die("Database selection also failed miserably: " . mysql_error());
        }

followed by

其次是

mysql_select_db("databaseiheko");

mysql_select_db("databaseiheko");

  1. Note the $dbnameand $dbvariables, you don't have them on your code, this function won't work like this.
  2. The second mysql_select_dboverwrites the first, but you don't specify a server connection to be used.
  1. 注意$dbname$db变量,你的代码中没有它们,这个函数不会像这样工作。
  2. 第二个mysql_select_db覆盖第一个,但您没有指定要使用的服务器连接。

You should use the first version, but you should use mysql_connectbefore it.

您应该使用第一个版本,但您应该mysql_connect在它之前使用。

You have typos in your code

你的代码有错别字

  • if (!db_select) {should be if (!$db_select) {
  • echo &row['DatumFuif']should be echo $row['DatumFuif']
  • if (!db_select) {应该 if (!$db_select) {
  • echo &row['DatumFuif']应该 echo $row['DatumFuif']

回答by Gaucho

mysql_ functions are deprecated, but if you want to use them, i suggest these corrections:

mysql_ 函数已弃用,但如果您想使用它们,我建议进行以下更正:

You can correct your code this way: the mysql connect is needed:

您可以通过这种方式更正代码:需要 mysql 连接:

 <?php
 //connect to your database
 mysql_connect("serverIpAddress","userName","password");
 //specify database
 mysql_select_db("yourDatabaseName") or die;
 //Build SQL Query
 $query = "select * from tblfuiven";
 $queryResult=mysql_query($query);
 $numrows=mysql_num_rows($queryResult);

numrows will contain the number of found records in the db. add an echo for the number of rows and let us know if the number of rows is still one. Then use mysql_fetch_assoc to get rows:

numrows 将包含数据库中找到的记录数。添加行数的回声,并让我们知道行数是否仍然为一。然后使用 mysql_fetch_assoc 获取行:

  while($row = mysql_fetch_assoc($queryResult)) {
        ?>
            <tr>
                <td><?php echo $row['NaamFuif']?></td>
                <td><?php echo $row['GemeenteFuif']?></td>
                <td><?php echo &row['DatumFuif']?></td>
            </tr>
        <?php

        }
        ?>

EDIT: You can test the code and let us know the number of rows that you obtain, using this code(write your real user name and password and db name:

编辑:您可以使用此代码测试代码并让我们知道您获得的行数(写下您的真实用户名和密码以及数据库名称:

         <?php
         mysql_connect("localhost","root","root");     
         mysql_select_db("databaseName") or die;
            $results = mysql_query("SELECT * FROM tblfuiven");
            $numrows=mysql_num_rows($queryResult);
           while($row = mysql_fetch_assoc($queryResult)) {
            ?>
                <tr>
                    <td><?php echo $numrows ?></td>
                    <td><?php echo $row['NaamFuif']?></td>
                    <td><?php echo $row['GemeenteFuif']?></td>
                    <td><?php echo $row['DatumFuif']?></td>
                </tr>

            <?php
           }
           ?>

回答by dave

Your problem is in the php echo statement. It is missing ";".

您的问题出在 php echo 语句中。它缺少“;”。