首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将SQL数据库中的数据显示到php/ html表中

将SQL数据库中的数据显示到php/ html表中
EN

Stack Overflow用户
提问于 2013-03-06 23:16:43
回答 2查看 398.7K关注 0票数 22

我在MySQL上有一个数据库,我想在一个HTML表或PHP表上显示我的一个SQL表。我已经在网上搜索过了,无法实现此功能。有没有人能帮我编一下代码?

代码语言:javascript
复制
database = 'hrmwaitrose'
username = 'root'
host = 'localhost'

没有密码。

我想显示"employee“表中的数据。

EN

回答 2

Stack Overflow用户

发布于 2013-03-06 23:18:36

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

代码语言:javascript
复制
<?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();
?>
票数 3
EN

Stack Overflow用户

发布于 2013-03-06 23:25:51

请参阅http://www.w3schools.com/php/php_mysql_select.asp。如果你是一个初学者并且想要学习,w3schools是一个很好的地方。

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15251095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档