0

我从另一个站点获得了一个函数来生成基于 PHP 查询的表:

function SQLResultTable($Query)
{
    $host = "localhost";
$user = "root";
$pass = "";
$db = "Quality_Monitoring";
    $link = mysql_connect($host, $user, $pass) or die('Could not connect: ' . mysql_error());      //build MySQL Link
    mysql_select_db($db) or die('Could not select database');        //select database
    $Table = "";  //initialize table variable

    $Table.= "<table id='Table1' border='1' style=\"border-collapse: collapse; text-align: center; font-size: 10px; cellspacing: 5px; \">"; //Open HTML Table

    $Result = mysql_query($Query); //Execute the query
    if(mysql_error())
    {
        $Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";
    }
    else
    {
        //Header Row with Field Names
        $NumFields = mysql_num_fields($Result);
        $Table.= "<tr style=\"background-color: #000066; text-align: center; color: #FFFFFF;\">";
        for ($i=0; $i < $NumFields; $i++)
        {     
            $Table.= "<th>" . mysql_field_name($Result, $i) . "</th>"; 
        }
        $Table.= "</tr>";

        //Loop thru results
        $RowCt = 0; //Row Counter
        while($Row = mysql_fetch_assoc($Result))
        {
            //Alternate colors for rows
            if($RowCt++ % 2 == 0) $Style = "background-color: #CCCCCC;";
            else $Style = "background-color: #FFFFFF;";

            $Table.= "<tr style=\"$Style\">";
            //Loop thru each field
            foreach($Row as $field => $value)
            {
                $Table.= "<td>$value</td>";
            }
            $Table.= "</tr>";
        }
       // $Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\"><td colspan='$NumFields'>Query Returned " . mysql_num_rows($Result) . " records</td></tr>";
    }
    $Table.= "</table>";

    return $Table;

}

我需要做的不是第一列的标题,而是将列保留在那里,其中包含记录。谁能帮我解决这个问题?说到 PHP,完全是菜鸟,而且真的不知道我在哪里/要修改什么。任何帮助表示赞赏,干杯!

4

1 回答 1

0

删除这部分代码:

    //Header Row with Field Names
    $NumFields = mysql_num_fields($Result);
    $Table.= "<tr style=\"background-color: #000066; text-align: center; color: #FFFFFF;\">";
    for ($i=0; $i < $NumFields; $i++)
    {     
        $Table.= "<th>" . mysql_field_name($Result, $i) . "</th>"; 
    }
    $Table.= "</tr>";

所以你的功能将是:

function SQLResultTable($Query)
{
    $host = "localhost";
$user = "root";
$pass = "";
$db = "Quality_Monitoring";
    $link = mysql_connect($host, $user, $pass) or die('Could not connect: ' . mysql_error());      //build MySQL Link
    mysql_select_db($db) or die('Could not select database');        //select database
    $Table = "";  //initialize table variable

    $Table.= "<table id='Table1' border='1' style=\"border-collapse: collapse; text-align: center; font-size: 10px; cellspacing: 5px; \">"; //Open HTML Table

    $Result = mysql_query($Query); //Execute the query
    if(mysql_error())
    {
    $Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";
    }
    else
    {
    //Loop thru results
    $RowCt = 0; //Row Counter
    while($Row = mysql_fetch_assoc($Result))
    {
        //Alternate colors for rows
        if($RowCt++ % 2 == 0) $Style = "background-color: #CCCCCC;";
        else $Style = "background-color: #FFFFFF;";

        $Table.= "<tr style=\"$Style\">";
        //Loop thru each field
        foreach($Row as $field => $value)
        {
            $Table.= "<td>$value</td>";
        }
        $Table.= "</tr>";
    }
       // $Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\"><td colspan='$NumFields'>Query Returned " . mysql_num_rows($Result) . " records</td></tr>";
    }
    $Table.= "</table>";

    return $Table;

}

但在这一点上,这个函数使用mysql_*了相当陈旧和(现在)讨厌的函数。您可能要考虑改用PDO 。

编辑:试试这个:

function SQLResultTable($Query)
{
    $host = "localhost";
$user = "root";
$pass = "";
$db = "Quality_Monitoring";
    $link = mysql_connect($host, $user, $pass) or die('Could not connect: ' . mysql_error());      //build MySQL Link
    mysql_select_db($db) or die('Could not select database');        //select database
    $Table = "";  //initialize table variable

    $Table.= "<table id='Table1' border='1' style=\"border-collapse: collapse; text-align: center; font-size: 10px; cellspacing: 5px; \">"; //Open HTML Table

    $Result = mysql_query($Query); //Execute the query
    if(mysql_error())
    {
        $Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";
    }
    else
    {
        //Header Row with Field Names
        $NumFields = mysql_num_fields($Result);
        $Table.= "<tr style=\"background-color: #000066; text-align: center; color: #FFFFFF;\">";
        for ($i=0; $i < $NumFields; $i++)
        {     
            $Table.= "<th>" . mysql_field_name($Result, $i) . "</th>"; 
        }
        $Table.= "</tr>";

        //Loop thru results
        $RowCt = 0; //Row Counter
        while($Row = mysql_fetch_assoc($Result))
        {
            //Alternate colors for rows
            if($RowCt!=0)
        {
            if($RowCt % 2 == 0) $Style = "background-color: #CCCCCC;";
            else $Style = "background-color: #FFFFFF;";

            $Table.= "<tr style=\"$Style\">";
            //Loop thru each field
            foreach($Row as $field => $value)
            {
                $Table.= "<td>$value</td>";
            }
            $Table.= "</tr>";
        }
    $RowCt++;
        }
       // $Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\"><td colspan='$NumFields'>Query Returned " . mysql_num_rows($Result) . " records</td></tr>";
    }
    $Table.= "</table>";

    return $Table;

}
于 2012-08-23T11:22:39.210 回答