使用MySQL/PHP结果构建HTML表 [英] Building a HTML table using MySQL/PHP results
问题描述
我正在尝试通过MySQL查询构建动态HTML表(无论列数或行数如何),但我认为我可能做错了什么,因为它根本无法工作.
I am trying to build a dynamic HTML table (regardless of number of columns or rows) from a MySQL query but I think I might be doing something wrong as it doesn't work at all.
我有使用Oracle的经验,但是我是MySQL的新手.我试图检查我正在使用的MySQL/PHP函数是否确实存在,我认为是这种情况.
I have experience with Oracle but I am new to MySQL. I have tried to check that the MySQL/PHP functions I am using do exist and I think that is the case.
<?php
$mysqli = new mysqli("localhost", "dbuser", "dbpass", "db");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT * FROM user")) {
/* build html table */
echo "<table class='table '><thread><tr>";
$ncols = mysqli_num_rows($result);
for ($i = 1; $i <= $ncols; $i++) {
$column_name = mysqli_field_name($result, $i);
echo "<th>".$column_name."</th>";
}
echo "</tr></thread><tbody>";
while ($row = mysqli_fetch_array($result, OCI_NUM)) {
echo "<tr>";
for ( $ii = 0; $ii < count($row); $ii++ ) {
echo "<td>" . $row[$ii] . "</td>";
}
echo "</tr>";
}
echo "</tbody></table>";
/* free result set */
$result->close();
}
$mysqli->close();
?>
感谢任何帮助!
史蒂夫
我按照建议添加了错误报告并得到:
I added the error reporting as suggested and get:
Fatal error: Call to undefined function mysqli_field_name() in connection.php on line 20
推荐答案
您的基本问题是,您可能已将mysql_
函数转换为mysqli
,这太好了!但是没有函数mysqli_field_name()
,该函数只是作为mysql_
函数存在.
Your basic problem is, that you probably converted from mysql_
functions to mysqli
which is great! But there is no function mysqli_field_name()
this function does just exist as a mysql_
function.
您需要 mysqli_result::fetch_fields()
函数/mysqli
中的方法来获取字段名称.
You need the mysqli_result::fetch_fields()
function/method in mysqli
to get the field names.
这篇关于使用MySQL/PHP结果构建HTML表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!