我有一个本地数据库文件,我想要一个html webform来查询.webform包含一个文本字段,用户输入一个SQL查询并提交,这在下面的代码中保存为$ sql。如果查询是“SELECT * from rc3 ..”,我就能够执行此操作,因为显示了所有列。但是,如果用户选择单个列,例如“SELECT new_ra,rc3_ra FROM rc3 ....”,则脚本会出错,因为未检索到其他列。我知道有一种方法可以通过获取列来实现,但我不确定为什么mysql_fetch_array,mysql_fetch_field不起作用。
这是我的代码:
<html>
<head>
<title>Query Results Page</title>
</head>
<body>
<?php
$sql= $_POST['sql'];
echo "Search by PGC number </br>";
$db = new PDO('sqlite:rc3.db');
$result = $db->prepare($sql);
$result->execute();
echo "<br>Result for:<br>".$sql."<br>";
echo "<br><table style='width:300px' border='1' cellpadding='10px'> ";
echo "<tr>";
echo "<td>PGC</td>";
echo "<td>rc3_ra</td>";
echo "<td>rc3_dec</td>";
echo "<td>rc3_radius</td>";
echo "<td>new_ra</td>";
echo "<td>new_dec</td>";
echo "<td>clean</td>";
echo "<td>error</td>";
echo "<td>in SDSS footprint</td>";
echo "<td>Low surface structure image</td>";
echo "<td>Poster image</td>";
echo "<td>ufits</td>";
echo "<td>gfits</td>";
echo "<td>rfits</td>";
echo "<td>ifits</td>";
echo "<td>zfits</td>";
echo "</tr>";
while($row = $result->fetchObject())
{
echo "<tr>";
echo " <td>".htmlspecialchars($row->PGC)."</td>";
echo " <td>".htmlspecialchars($row->rc3_ra)."</td>";
echo " <td>".htmlspecialchars($row->rc3_dec)."</td>";
echo " <td>".htmlspecialchars($row->rc3_radius)."</td>";
echo " <td>".htmlspecialchars($row->new_ra)."</td>";
echo " <td>".htmlspecialchars($row->new_dec)."</td>";
echo " <td>".htmlspecialchars($row->clean)."</td>";
echo " <td>".htmlspecialchars($row->error)."</td>";
echo " <td>".htmlspecialchars($row->in_SDSS_footprint)."</td>";
echo "<td><a href='".htmlspecialchars($row->low)."'download='".htmlspecialchars($row->low)."' target='_blank'>".explode("/", htmlspecialchars($row->low))[2]."</a></td>";
echo "<td><a href='".htmlspecialchars($row->best)."'download='".htmlspecialchars($row->best)."' target='_blank'>".explode("/", htmlspecialchars($row->best))[2]."</a></td>";
echo "<td><a href='".htmlspecialchars($row->ufits)."'download='".htmlspecialchars($row->ufits)."' target='_blank'>".explode("/", htmlspecialchars($row->ufits))[2]."</a></td>";
echo "<td><a href='".htmlspecialchars($row->gfits)."'download='".htmlspecialchars($row->gfits)."' target='_blank'>".explode("/", htmlspecialchars($row->gfits))[2]."</a></td>";
echo "<td><a href='".htmlspecialchars($row->rfits)."'download='".htmlspecialchars($row->rfits)."' target='_blank'>".explode("/", htmlspecialchars($row->rfits))[2]."</a></td>";
echo "<td><a href='".htmlspecialchars($row->ifits)."'download='".htmlspecialchars($row->ifits)."' target='_blank'>".explode("/", htmlspecialchars($row->ifits))[2]."</a></td>";
echo "<td><a href='".htmlspecialchars($row->zfits)."'download='".htmlspecialchars($row->zfits)."' target='_blank'>".explode("/", htmlspecialchars($row->zfits))[2]."</a></td>";
echo "</tr>";
}
echo "<table>";
?>
</body>
</html>
答案 0 :(得分:0)
我暂时没有使用过PDO,所以可能会采用一种稍微简洁的方式来做到这一点,但是,这应该仍然有效:
$sql = $_POST['sql'];
echo "Search by PGC number </br>";
$db = new PDO('sqlite:rc3.db');
$result = $db->prepare($sql);
$result->execute();
echo "<br>Result for:<br>" . $sql . "<br>";
echo "<br><table style='width:300px' border='1' cellpadding='10px'> <thead> ";
$firstRun = TRUE;
$links = array('low', 'best', 'ufits', 'gfits', 'rfits', 'ifits', 'zfits');
while ($row = $result->fetchObject()) {
if ($firstRun !== FALSE) {
echo '<tr>';
foreach ($row as $th => $val) {
echo "<th>$th</th>";
}
echo '</tr></thead><tbody>';
$firstRun = FALSE;
}
echo '<tr>';
foreach ($row as $key => $value) {
$row_value = htmlspecialchars($value);
if (in_array($key, $links)) {
$link_val = explode('/', $row_value);
echo '<td><a href="' . $row_value . '" download="' . $row_value . ' target="_blank"">' . $link_val[2] . '</a></td>';
} else {
echo '<td>' . $row_value . '</td>';
}
}
}
echo '</tr>';
echo "</tr>";
}
echo '</tbody></table>';
希望这有帮助!