php生成的表的第1列没有标题
我从另一个站点获得了一个函数,用于根据PHP查询生成表:php生成的表的第1列没有标题,php,html-table,Php,Html Table,我从另一个站点获得了一个函数,用于根据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
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;
}
函数sqlresultable($Query)
{
$host=“localhost”;
$user=“root”;
$pass=“”;
$db=“质量监控”;
$link=mysql\u connect($host,$user,$pass)或die('cannotconnect:'。mysql\u error());//构建mysql链接
mysql_select_db($db)或die('not select database');//选择数据库
$Table=”“;//初始化表变量
$Table.=“”;//打开HTML表格
$Result=mysql\u query($query);//执行查询
if(mysql_error())
{
$Table.=“MySQL错误:”.MySQL_ERROR();
}
其他的
{
//带有字段名的标题行
$NumFields=mysql\u num\u字段($Result);
$Table.=”;
对于($i=0;$i<$NumFields;$i++)
{
$Table.=''.mysql_字段_名称($Result,$i)。'';
}
$Table.=”;
//循环通过结果
$RowCt=0;//行计数器
while($Row=mysql\u fetch\u assoc($Result))
{
//行的交替颜色
如果($RowCt++%2==0)$Style=“背景色:#cccc;”;
else$Style=“背景色:#FFFFFF;”;
$Table.=”;
//循环通过每个字段
foreach($行作为$字段=>$值)
{
$Table.=“$value”;
}
$Table.=”;
}
//$Table.=“查询返回”。mysql\u num\u行($Result)。“记录”;
}
$Table.=”;
返回$Table;
}
我需要做的不是为第一列设置标题,而是将列保留在那里,其中包含记录。有谁能帮我解决这个问题吗?说到PHP,我完全不知道我想修改什么地方。感谢您的帮助,干杯 删除这段代码:
//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>";
啊,对不起,我没有很好地解释我自己,我确实需要标题行,只需要第一列标题为空。@ChrisSpalton我不确定我是否理解,你想在标题行中显示什么?只需要第一列,('column A',如果你喜欢)在标题行中没有值,列B,C,D等等,需要正常的标题。不,抱歉,伙计,这只是将表格限制为标题行加上1行数据。还保留了列标题。@ChrisSpalton Dammit,他们粘贴了
++。已更新编辑。请不要使用mysql.*
函数编写新代码。它们不再得到维护,社区已经开始。看到了吗?相反,你应该学习并使用或。如果你不能决定哪一个,我会帮你的。如果你选择PDO,@Truth allready说,请使用一些标准且经过良好测试的framworks/库。许多人可能已经在不同的条件和环境下对它们进行了测试。
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;
}
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;
}