从sql表创建HTML表 [英] Create HTML table from sql table

查看:94
本文介绍了从sql表创建HTML表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL数据库中有一个包含以下字段的表:ID,姓名,电子邮件,大学,语言和经验。我想创建一个从SQL获取数据并输出最后10个结果的html表?我会怎么做?



如果这是一个非常简单的问题,我很抱歉,我对PHP和SQL知之甚少。



下面是我现在刚刚显示名称而不是表格中的代码:

 < html> 
< head>
< title>最后5个结果< / title>
< / head>
< body>
<?php
$ connect = mysql_connect(localhost,root,root);
if(!$ connect){
die(mysql_error());
}
mysql_select_db(apploymentdevs);
$ results = mysql_query(SELECT * FROM demo);
while($ row = mysql_fetch_array($ results)){

echo $ row ['Name']。 < / BR> 中;


?>
< / body>
< / html>


解决方案

以下是应该帮助您创建表的内容并获得有关 php mysql 另外你应该把你的连接逻辑和查询移到开头你的过程,从而避免加载页面时出现错误,并显示比mysql_error更准确的错误。



编辑:如果你的ID正在递增,那么你可以添加ORDER BY子句,

更改: SELECT * FROM demo LIMIT 10
to: SELECT * FROM demo LIMIT 10 ORDER BY id

 < html> 
< head>
< title>最后10个结果< / title>
< / head>
< body>
< table>
< thead>
< tr>
< td> Id< / td>
< td>名称< / td>
< / tr>
< / thead>
< tbody>
<?php
$ connect = mysql_connect(localhost,root,root);
if(!$ connect){
die(mysql_error());
}
mysql_select_db(apploymentdevs);
$ results = mysql_query(SELECT * FROM demo LIMIT 10);
while($ row = mysql_fetch_array($ results)){
?>
< tr>
< td><?php echo $ row ['Id']?>< / td>
< td><?php echo $ row ['Name']?>< / td>
< / tr>

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


I have a table in a SQL database with the following fields: ID, Name, Email, University, Languages, and Experience. I want to create an html table that fetches data from SQL and outputs the last 10 results? How would I do that?

I'm sorry if this is a very simple question, I have very little knowledge in PHP and SQL.

Here's the code I have right now that just displays the name and not in a table:

    <html>
    <head>
        <title>Last 5 Results</title>
    </head>
    <body>
        <?php
            $connect = mysql_connect("localhost","root", "root");
            if (!$connect) {
                die(mysql_error());
            }
            mysql_select_db("apploymentdevs");
            $results = mysql_query("SELECT * FROM demo");
            while($row = mysql_fetch_array($results)) {

                echo $row['Name'] . "</br>";


            ?>
    </body>
</html>

解决方案

Here is something that should help you to create the table and get more knowledge of php and mysql.

Also you should move your connection logic and query to the beginning of your process, thus avoiding errors while loading the page and showing a more accurate error than just the mysql_error.

Edit: If your ids are incrementing, then you could add the ORDER BY clause,
change: SELECT * FROM demo LIMIT 10 to: SELECT * FROM demo LIMIT 10 ORDER BY id

<html>
    <head>
        <title>Last 10 Results</title>
    </head>
    <body>
        <table>
        <thead>
            <tr>
                <td>Id</td>
                <td>Name</td>
            </tr>
        </thead>
        <tbody>
        <?php
            $connect = mysql_connect("localhost","root", "root");
            if (!$connect) {
                die(mysql_error());
            }
            mysql_select_db("apploymentdevs");
            $results = mysql_query("SELECT * FROM demo LIMIT 10");
            while($row = mysql_fetch_array($results)) {
            ?>
                <tr>
                    <td><?php echo $row['Id']?></td>
                    <td><?php echo $row['Name']?></td>
                </tr>

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

这篇关于从sql表创建HTML表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆