gpt4 book ai didi

php - 如何获取mysql列名和值

转载 作者:行者123 更新时间:2023-11-29 01:59:20 26 4
gpt4 key购买 nike

我正在尝试动态获取列名和值,这意味着如果在数据库中添加了新列,我想在页面刷新时在前端显示

下面会得到列名,但我需要检索与列名相关的值。谁能指导我如何做,谢谢。

  <?php

Include"db.php";


echo "<table>";
echo "<tr>";
$qColumnNames = mysql_query("SHOW COLUMNS FROM fullnames") or die("mysql error");


$numColumns = mysql_num_rows($qColumnNames);
$x = 0;
while ($x < $numColumns)
{
$colname = mysql_fetch_row($qColumnNames);
$col[$colname[0]] = $colname[0];
$x++;
}

foreach($col as $head){
echo "<th>$head</th>";

}



echo "</tr>";
echo "</table>";

?>

最佳答案

可以做这样的事情:(前提是你的检索列名的函数有效)

编辑代码

Include("db.php");


echo "<table>";

$qColumnNames = mysql_query("SHOW COLUMNS FROM fullnames") or die("mysql error");


$numColumns = mysql_num_rows($qColumnNames);
$x = 0;

while ($x < $numColumns)
{
$colname = mysql_fetch_row($qColumnNames);
$col[$colname[0]] = $colname[0];
$x++;
}

$result = mysql_query("SELECT * FROM fullnames");
$counter = 0;
while($row = mysql_fetch_assoc($result)){
foreach($col as $c){
$rows[$counter][] = "<td>" . $row[$c] . "</td>";
}
$counter++;
}

echo "<tr>";
foreach($col as $c){
//echo headers
echo "<th>" . $c . "</th>";
}
echo "</tr>";
foreach($rows as $r){
//Echo all content
echo "<tr>";
foreach($r as $cell){
//echo all cells
echo $cell;
}
echo "</tr>";
}
echo "</table>";

?>

还没有时间测试这个,但它应该可以工作。(但您应该改用 msqli_ 函数。

关于php - 如何获取mysql列名和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19206652/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com