【问题标题】:display data from SQL database into php/ html table [closed]将 SQL 数据库中的数据显示到 php/html 表中 [关闭]
【发布时间】:2013-02-21 11:09:52
【问题描述】:

我有一个 MySQL 数据库,我想在 HTML 或 PHP 表上显示我的一个 SQL 表。我在网上搜索过,无法实现这个功能。有人可以帮我编码吗?

database = 'hrmwaitrose'
username = 'root'
host = 'localhost'

没有密码。

我想显示“员工”表中的数据。

【问题讨论】:

  • 您需要的不仅仅是 StackOverflow 所能提供的。我建议买一本关于 PHP 入门的书,或者在 Google 上找到类似“PHP SQL 教程”之类的东西。
  • 你真的在网上搜索过吗?

标签: php html mysql


【解决方案1】:

看手册http://www.php.net/manual/en/mysqli.query.php

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

/* Create table doesn't return a resultset */
if ($mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
    printf("Table myCity successfully created.\n");
}

/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) {
    printf("Select returned %d rows.\n", $result->num_rows);

    /* free result set */
    $result->close();
}

/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result = $mysqli->query("SELECT * FROM City", MYSQLI_USE_RESULT)) {

    /* Note, that we can't execute any functions which interact with the
       server until result set was closed. All calls will return an
       'out of sync' error */
    if (!$mysqli->query("SET @a:='this will not work'")) {
        printf("Error: %s\n", $mysqli->error);
    }
    $result->close();
}

$mysqli->close();
?>

【讨论】:

    【解决方案2】:

    参考http://www.w3schools.com/php/php_mysql_select.asp。 如果您是初学者并想学习,w3schools 是个不错的地方。

    <?php
        $con=mysqli_connect("localhost","root","YOUR_PHPMYADMIN_PASSWORD","hrmwaitrose");
        // Check connection
        if (mysqli_connect_errno())
          {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
          }
    
        $result = mysqli_query($con,"SELECT * FROM employee");
    
        while($row = mysqli_fetch_array($result))
          {
          echo $row['FirstName'] . " " . $row['LastName']; //these are the fields that you have stored in your database table employee
          echo "<br />";
          }
    
        mysqli_close($con);
        ?>
    

    你也可以在你的桌子里面echo

    <?php
     echo "<table>";
     while($row = mysqli_fetch_array($result))
              {
              echo "<tr><td>" . $row['FirstName'] . "</td><td> " . $row['LastName'] . "</td></tr>"; //these are the fields that you have stored in your database table employee
              }
     echo "</table>";
     mysqli_close($con);
    ?>
    

    【讨论】:

      【解决方案3】:

      PHP 提供了连接 MySQL 数据库的函数。

      $connection = mysql_connect('localhost', 'root', ''); //The Blank string is the password
      mysql_select_db('hrmwaitrose');
      
      $query = "SELECT * FROM employee"; //You don't need a ; like you do in SQL
      $result = mysql_query($query);
      
      echo "<table>"; // start a table tag in the HTML
      
      while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
      echo "<tr><td>" . htmlspecialchars($row['name']) . "</td><td>" . htmlspecialchars($row['age']) . "</td></tr>";  //$row['index'] the index here is a field name
      }
      
      echo "</table>"; //Close the table in HTML
      
      mysql_close(); //Make sure to close out the database connection
      

      在 while 循环(每次遇到结果行时都会运行)中,我们 echo 创建了一个新的表行。我还添加了一个来包含字段。

      这是一个非常基本的模板。您会看到使用 mysqli_connect 而不是 mysql_connect 的其他答案。 mysqli 代表 mysql 改进。它提供了更好的功能范围。你注意到它也有点复杂。这取决于你需要什么。

      请注意,“mysql_fetch_array”自 PHP 5.5.0 起已被弃用,并在 PHP 7.0.0 中被删除。所以请改为查看“mysqli_fetch_array()”。

      【讨论】:

      • 谢谢!这段代码有效,但是,你知道我如何将数据插入到表中>?并进入不同的行?
      • @user2108411 更新了答案
      • 我试过了,但对我没有用...我的意思是我得到了我的桌子并且行数是正确的。但是,当我使用 $row['ColumnName']... 时,我不断得到列的实际名称,而不是数据本身。
      • 你能把你的代码发布到 gisthub 或类似的地方吗?
      • 警告: mysql_* 扩展自 PHP 5.5.0 起已弃用,自 PHP 7.0.0 起已被删除。相反,应该使用mysqliPDO_MySQL 扩展名。在选择 MySQL API 时,另请参阅 MySQL API Overview 以获得更多帮助。
      【解决方案4】:

      这是我编写的一个简单函数,用于显示表格数据,而无需输入每个列名: (另外,请注意:嵌套循环)

      function display_data($data) {
          $output = '<table>';
          foreach($data as $key => $var) {
              $output .= '<tr>';
              foreach($var as $k => $v) {
                  if ($key === 0) {
                      $output .= '<td><strong>' . $k . '</strong></td>';
                  } else {
                      $output .= '<td>' . $v . '</td>';
                  }
              }
              $output .= '</tr>';
          }
          $output .= '</table>';
          echo $output;
      }
      

      更新功能如下

      嗨,杰克,

      你的函数设计很好,但是这个函数总是错过数组中的第一个数据集。我对此进行了测试。

      你的函数很好,很多人会使用它,但他们总是会错过第一个数据集。这就是我写这个修正案的原因。

      如果 key === 0,则缺少数据集。如果 key = 0,则仅写入列标题,但不写入包含 $key 0 的数据。所以总是缺少数组的第一个数据集。

      您可以通过将 if 条件移到第二个 foreach 循环上方来避免这种情况,如下所示:

      function display_data($data) {
          $output = "<table>";
          foreach($data as $key => $var) {
              //$output .= '<tr>';
              if($key===0) {
                  $output .= '<tr>';
                  foreach($var as $col => $val) {
                      $output .= "<td>" . $col . '</td>';
                  }
                  $output .= '</tr>';
                  foreach($var as $col => $val) {
                      $output .= '<td>' . $val . '</td>';
                  }
                  $output .= '</tr>';
              }
              else {
                  $output .= '<tr>';
                  foreach($var as $col => $val) {
                      $output .= '<td>' . $val . '</td>';
                  }
                  $output .= '</tr>';
              }
          }
          $output .= '</table>';
          echo $output;
      }
      

      最好的问候和感谢 - Axel Arnold Bangert - Herzogenrath 2016

      还有另一个更新删除了影响代码可维护性的冗余代码块。

      function display_data($data) {
      $output = '<table>';
      foreach($data as $key => $var) {
          $output .= '<tr>';
          foreach($var as $k => $v) {
              if ($key === 0) {
                  $output .= '<td><strong>' . $k . '</strong></td>';
              } else {
                  $output .= '<td>' . $v . '</td>';
              }
          }
          $output .= '</tr>';
      }
      $output .= '</table>';
      echo $output;
      

      }

      【讨论】:

      • 你不能删除 else 子句吗?这样你每一行都会正确呈现。
      • 用于显示列字段名
      • 喜欢保留 $output .= '' 。 $v 。 '';但只需取出其他。
      • 仍然没有检索到第一个数据集
      猜你喜欢
      • 2018-04-25
      • 2017-08-15
      • 1970-01-01
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 2019-03-10
      相关资源
      最近更新 更多