【问题标题】:How to display data from SQL database into PHP/HTML table如何将 SQL 数据库中的数据显示到 PHP/HTML 表中
【发布时间】:2017-08-15 08:30:36
【问题描述】:

所以我已经在使用 post 将 HTML 表单中的数据插入到在 XAMPP 上运行的 MySQL 数据库中,然后我将如何在表格中的另一个 HTML 页面上显示这些数据?当我尝试从 localhost 运行它时,它会出现一个空白页面,顶部有一行代码。我是新手,这是我的代码:我做得对吗?

<html>
<head>
</head>
<body>
    <?php
    $con = mysql_connect('localhost', 'root', '');
    if (!$con){
        die("Can not connect: " . mysql_error());
    }
    mysql_select_db("form_process", $con);
    $sql = "SELECT * FROM `form_submissions`";
    $myData = mysql_query($sql,$con);
    echo "<table border=1>
    <tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Phone Number</th>
    <th>Class interested in</th>
    </tr>"; 

    while($row= mysql_fetch_array($result)){
        echo "<tr>";
        echo "<td>" . $record['First'] . "</td>";
        echo "<td>" . $record['Last'] . "</td>";
        echo "<td>" . $record['Phone'] . "</td>";
        echo "<td>" . $record['Class'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";

    mysqli_close();

    ?>

</body>
</html>

【问题讨论】:

  • 尝试:while($row= mysql_fetch_array($myData)){ - 变量$result 未以任何方式设置。尝试阅读您的错误日志,它会让您知道此类问题:)
  • 每次在新代码中使用the mysql_ 数据库扩展a Kitten is strangled somewhere in the world 时,它都会被弃用,并且已经存在多年,并且在 PHP7 中永远消失了。如果您只是学习 PHP,请花精力学习 PDOmysqli 数据库扩展和预处理语句。 Start here
  • error reporting 添加到您的文件顶部测试时 在您打开PHP 标记之后,例如&lt;?php error_reporting(E_ALL); ini_set('display_errors', 1); 以查看它是否产生任何结果。然后你会看到你的错误,以便你修复它们

标签: javascript php html mysql xampp


【解决方案1】:

试试下面的代码,看看 cmets 的程序变化

<html>
  <head>
  </head>
  <body>
    <?php
      // MySQL has been deprecated so use mysqli or pdo.
      $con = mysqli_connect('localhost', 'root', '', 'form_process') die("Can not connect: " . mysql_error());

      $sql = "SELECT * FROM `form_submissions`";
      $myData = mysqli_query($con, $sql);

      echo "<table border=1>
        <tr>
          <th>First Name</th>
          <th>Last Name</th>
          <th>Phone Number</th>
          <th>Class interested in</th>
        </tr>"; 

      // mysqli_fetch_array should have query result in parameters
      while($row = mysqli_fetch_array($myData)){
        echo "<tr>";
        // use proper array in this case its $row as in while condition
        echo "<td>" . $row['First'] . "</td>";
        echo "<td>" . $row['Last'] . "</td>";
        echo "<td>" . $row['Phone'] . "</td>";
        echo "<td>" . $row['Class'] . "</td>";
        echo "</tr>";
      }
      echo "</table>";

      mysqli_close();

    ?>

  </body>
</html>

【讨论】:

  • 您好,感谢回复,使用此代码时提示错误:Parse error: syntax error, unexpected 'die' (T_EXIT)
  • 你知道为什么会发生这种情况以及如何摆脱这个错误吗? @sachinPATIL
  • 抱歉有错误。将行替换为 $con = mysqli_connect('localhost', 'root', '', 'form_process'); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } 。该问题是由于语法错误而出现的。
【解决方案2】:

你应该使用 mysqli 或 pdo , mysql 已被弃用。 mysqli 类似于 mysql。

mysqli代码如下

  $con = mysqli_connect('localhost', 'root', '', 'form_process') die("Can not connect: " . mysql_error());

  $sql = "SELECT * FROM `form_submissions`";
  $myData = mysqli_query($con, $sql);

  while($row = mysqli_fetch_array($myData)){
  /// some code
  }
  mysqli_close();

【讨论】:

  • 使用了这段代码,但是当我尝试通过 localhost 访问它时出现错误:Parse error: syntax error, unexpected 'die' (T_EXIT) in C:\xampp\htdocs\retrieve_data.php在第 6 行@faysalAhammed
猜你喜欢
  • 2018-04-25
  • 2013-02-21
  • 1970-01-01
  • 2014-01-15
  • 2019-07-05
  • 2018-09-24
  • 2017-04-16
  • 1970-01-01
  • 2021-09-09
相关资源
最近更新 更多