我有表MYTABLE包含字段:ID,PASSWORD,COL1,COL2,COL3,DATE 我想获取并显示HTML表中的所有记录,但跳过ID和密码字段... 我使用下面的代码是不工作:PHP MySQL如何跳过显示字段
$query = "SELECT * FROM MYTABLE WHERE 1";
$results = mysql_query($query, $conn) or die(mysql_error());
echo "<tr>";
while ($r = mysql_fetch_assoc($results)) {
foreach ($r as $item) {
// let's not display ID and password
if ($r == 'ID' || $r == 'PASSWORD') continue; // this is the line that I want to figure out
echo "<td>$item</td>";
} // end for loop
} // end while
unset($item);
echo "</tr>";
显然有不止1点的方式来做到这一点,比如我可以用替换foreach循环的循环:
for ($i=0;$i<=6;$i++) {
if ($i == 0 || $i == 1) continue;
echo "<td>$r[$i]</td>";
} // end for
这会跳过ID和PASSWOR D字段,但我不想使用它,因为我在多个表上运行代码(表名是从html select标记中提取的),并且这些表可能没有相同数量的字段/列(但它们将始终有ID和PASSWORD)。我也可以用SQL语句(我不想),但是然后我将不得不查询到一个临时工,删除ID,PASSWORD列,然后从临时表中获取。 (顺便说一下有没有令人信服的理由,为什么我其实应该与SQL Rathen市的比PHP做呢?)
为什么你就不能使用 “从MYTABLE选择COL1,COL2,COL3,DATE”? – Akhil 2014-10-09 12:24:23
因为他很伤心:“这会跳过ID和PASSWORD字段,但我不想使用它,因为我在多个表上运行代码(表名是从html选择标记中提取的)” – vaso123 2014-10-09 12:27:27