在过滤结果后,如何阻止我的表格打印多个列标题?示例如下所示
我的代码
function fillQueryData() {
$dbName = $_REQUEST ['db'];
$link = mysqli_connect ( '192.168.2.113', 'root', '', $dbName );
if (! $link) {
die ( "Connection failed" . mysqli_errno ( $link ) );
}
if (isset ( $_POST ['name'] )) {
$name = trim ( $_POST ['name'] );
$tblName = $_REQUEST ['tbl'];
$colqry = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '$dbName' AND TABLE_NAME = '$tblName'";
$col_names = mysqli_query ( $link, $colqry );
while ( $col = mysqli_fetch_array ( $col_names, MYSQLI_ASSOC ) ) {
$colName = $col ['COLUMN_NAME'];
$tblQry = "SELECT * FROM $tblName WHERE $colName='$name' LIMIT 15";
$query2 = mysqli_query ( $link, $tblQry );
echo "<table><tr>";
$i = 0;
while ( $i < mysqli_num_fields ( $query2 ) ) {
$meta = mysqli_fetch_field_direct ( $query2, $i );
echo "<th>" . $meta->name . "</th>";
$i = $i + 1;
}
echo "</tr>";
$i = 0;
while ( $row = mysqli_fetch_row ( $query2 ) ) {
echo "<tr>";
$count = count ( $row );
$y = 0;
while ( $y < $count ) {
$c_row = current ( $row );
echo "<td>" . $c_row . "</td>";
next ( $row );
$y = $y + 1;
}
echo "</tr>";
$i = $i + 1;
}
echo "</table>";
} }
}
我不希望在循环中将循环中的表数据放入我的列名称中,但如果我不想在行中得到任何结果。如何解决此问题,以便只显示过滤结果?
答案 0 :(得分:0)
列标题循环应该在数据获取循环之外
代码应该是这样的。
function filterTblData() {
$dbName = $_REQUEST ['db'];
$link = mysqli_connect ( '192.168.2.113', 'root', '', $dbName );
if (! $link) {
die ( "Connection failed" . mysqli_errno ( $link ) );
}
if (isset ( $_POST ['name'] )) {
$name = trim ( $_POST ['name'] );
$tblName = $_REQUEST ['tbl'];
// Dynamically select the column names of the chosen table.
$colqry = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '$dbName' AND TABLE_NAME = '$tblName'";
$col_names = mysqli_query ( $link, $colqry );
$col = mysqli_fetch_array ( $col_names, MYSQLI_ASSOC );
$colName = $col ['COLUMN_NAME'];
// Selecting and filtering results based on matching search terms.
$tblQry = "SELECT * FROM $tblName WHERE $colName='$name' LIMIT 15";
$query2 = mysqli_query ( $link, $tblQry );
echo "<table><tr>";
// Loop for fetching table column headers and riprinting them.
$i = 0;
while ( $i < mysqli_num_fields ( $query2 ) ) {
$meta = mysqli_fetch_field_direct ( $query2, $i );
echo "<th>" . $meta->name . "</th>";
$i = $i + 1;
}
echo "</tr>";
// Fetch row data and populate
while ( $col = mysqli_fetch_array ( $col_names, MYSQLI_ASSOC ) ) {
$colName = $col ['COLUMN_NAME'];
// Selecting and filtering results based on matching search terms.
$tblQry = "SELECT * FROM $tblName WHERE $colName='$name' LIMIT 15";
$query2 = mysqli_query ( $link, $tblQry );
$i = 0;
while ( $row = mysqli_fetch_row ( $query2 ) ) {
echo "<tr>";
$count = count ( $row );
$y = 0;
while ( $y < $count ) {
$c_row = current ( $row );
echo "<td>" . $c_row . "</td>";
next ( $row );
$y = $y + 1;
}
echo "</tr>";
$i = $i + 1;
}
}
echo "</table>";
}
}
filterTblData();