php - 使用 PHP 文件从 PHP MyAdmin 中选择表
问题描述
美好的一天,我正在尝试从 MySQL 中选择一个表,通常我使用以下代码:
$sql="CALL selectCreatedTableByName('".$tableNameIn."')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table id='restable'>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['nameE'] . "</td>";
echo "<td>" . $row['nameN'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
$conn->close();
但是现在,当我不知道表格列的名称时,我该如何选择这个表格?
非常感谢。
解决方案
您不是在选择表格,而是从表格中选择。(至少我猜是这种情况,因为我们不知道您的程序实际上是做什么的)。
返回的 PHP 数组使用结果集中的属性名称作为键,因此......
while($row = $result->fetch_assoc()) {
echo "<tr>\n";
foreach($row as $name=>$value) {
echo "<td>$value</td>\n";
}
echo "</tr>\n";
}
如果您想要标题行,请使用状态变量标记第一行。
$fetched=0;
while($row = $result->fetch_assoc()) {
if (!fetched) {
echo "<tr>\n";
foreach($row as $name=>$value) {
echo "<th>$name</th>\n";
}
echo "</tr>\n";
}
$fetched++;
echo "<tr>\n";
foreach($row as $name=>$value) {
echo "<td>$value</td>\n";
}
echo "</tr>\n";
}
推荐阅读
- php - Symfony 仅在集合的第一行呈现表单
- node.js - Node Express“favicon.ico”未找到错误
- magento - Magento 1.9.3.9 上的万事达卡支付网关服务
- python - 数据库问题 sqlite3.Flask 操作错误
- r - 从 R 中的人口统计包创建 demogdata
- c# - Xamarin.Android 和用户可下载的可执行代码 (.dll)
- javascript - 如何在node.js中链接css文件
- ms-access-2010 - 如何转换交叉表查询
- node.js - 如何使用 express-handlbars 将数据对象传递给我的视图?
- ssis - 使用 SSDT 2017 创建 SSIS 包