gpt4 book ai didi

php - 使用下拉菜单显示表和值

转载 作者:行者123 更新时间:2023-11-29 11:44:33 24 4
gpt4 key购买 nike

在此应用程序中,我有一个下拉列表,以便您可以选择要查看的表。使用 Ajax,我将变量 $tbl 作为整数推送,下面的代码打印出与该整数对应的表。但有些东西不起作用,我需要一双额外的眼睛来帮助调试。

if($tbl == 9){$table = "person";}

$conn = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
$query = mysqli_query($conn, "SELECT * FROM $table ") or die(mysqli_error($conn));

if($query){

echo '<table border="1"><thead><tr>';

$colnames = array();

while ($finfo = mysqli_fetch_field($query)) {
$name=$finfo->name;

array_push($colnames, $name);

echo "<th>".$name."</th>";

}
mysqli_free_result($query);

echo '</tr></thead><tbody>';



$count = 0;
echo $colnames[$count]; // this test prints the correct column name but it ends up being printed OUTSIDE the table for some reason
while ($row = mysqli_fetch_array($query, MYSQLI_BOTH)){
echo '<td>'.$row[$colnames[$count]].'</td>';
$count++;
}

echo "</tbody></table>";

}

最后一个 while 循环应该回显每个字段值,但它没有执行 <tbody>留空。

最佳答案

您太早调用了mysqli_free_result($query)。所以当你调用mysqli_fetch_array时,sql结果已经消失了。将 mysqli_free_result 移到最后一个循环之后。

关于php - 使用下拉菜单显示表和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35186157/

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