简体   繁体   English

mySQL转PHP动态表

[英]mySQL to PHP dynamic table

This question has been asked many times but I'm sorry I still can't make it work. 这个问题已经被问过很多次了,但是对不起,我仍然无法解决这个问题。 I have a simple mySQL DB like this table: 我有一个简单的mySQL DB,如下表:

在此处输入图片说明

In this DB I have a column with people and 3 other columns that represent days - Day 1, Day 2 and Day 3. The letter "M" means Morning and "A" Afternoon. 在此数据库中,我有一列人,其他三列分别代表天-第1天,第2天和第3天。字母“ M”表示早晨,“ A”下午。 So I want to query this DB and ask: "Who was working Day 1 in the morning?" 因此,我想查询该数据库并询问:“谁在第一天的早上工作?” and it returns "John". 它返回“ John”。 I know how to do this query, but I don't know how to format the return. 我知道如何执行此查询,但是我不知道如何格式化返回值。 Let's say that 3 people worked Day 1 morning (because the actual DB is bigger then this one) I want the HTML to return a 3 row table stating their names, but if only 2 persons worked Day one in the morning, it would return only a 2 row table stating their names. 假设第一天早上有3个人工作(因为实际的DB会比这个大),我希望HTML返回一个3行表来说明他们的名字,但是如果只有2个人在早上的第一天工作,它将只返回2行表说明其名称。

This is my PHP: 这是我的PHP:

<?php
    $con = mysql_connect(".",".",".");
    $db_selected = mysql_select_db("xbizh_14391723_horario", $con);

    $sql = "SELECT  `AM` FROM HORARIO WHERE  `1` ='N'";

    $result = mysql_query($sql);
    $num = mysql_numrows($result);


    if (!$result) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $query;
        die($message);
     }

    while($row = mysql_fetch_array($result))
    {
        print_r($row);
    }

    mysql_free_result($result);

?>

It seems to me you are most of the way there already. 在我看来,您已经完成了大部分工作。

print("<table>");

while($row = mysql_fetch_array($result))
{
    print("<tr><td>" . $row['name_field'] . "</td></tr>");
}

print("</table>");

You also need to make sure your query is returning the name field. 您还需要确保查询返回的是名称字段。

$sql = "SELECT * FROM HORARIO WHERE  `1` ='N'";

or 要么

$sql = "SELECT `name_field` FROM HORARIO WHERE  `1` ='N'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM