我有一个页面,它将SQL查询的一些结果返回给MysQL数据库.出于某种原因,它会返回每个值两次,我认为因为它存储在数据库中,包括数字索引和字符串索引.如何只提取具有字符串索引的索引和值,并忽略具有整数索引的索引和值?
我用来生成表的函数如下:
function SQLtable($answer) {
$result = "<table class=\"SQLresult\">";
$row = mysql_fetch_array($answer);
//TODO check for empty table here and return a message if empty
$result .= "<tr>";
foreach (array_keys($row) AS $heading) {
$result .= "<th>$heading</th>";
}
$result .= "</tr>";
do {
$result .= "<tr>";
foreach($row as $cell) {
$result .= "<td>".$cell."</td>";
}
$result .= "</tr>";
}
while ($row = mysql_fetch_array($answer));
$result .= "</table>";
return $result;
}
Run Code Online (Sandbox Code Playgroud)
结果截图:http://i.stack.imgur.com/3YXoh.png
谢谢.
edit1 - 我正在使用的SQL有点像测试的占位符.它是:
$query = "SELECT * FROM politicians";
Run Code Online (Sandbox Code Playgroud)
我也尝试过明确地询问列,我也遇到了同样的问题.
$query = "SELECT firstname,lastname,jurisdiction,party,riding FROM politicians";
Run Code Online (Sandbox Code Playgroud)
mysql_fetch_array()默认情况下,将返回包含关联数组和行的常规数字键控结果集的哈希.要获取一个或另一个使用mysql_fetch_row()(数组)或mysql_fetch_assoc()(哈希),或使用可选的第二个参数指定所需的类型mysql_fetch_array(),如其手册页(上面链接)中所述.
这些是等价的:
mysql_fetch_assoc($result) -> mysql_fetch_array($result, MYSQL_ASSOC);
mysql_fetch_row($result) -> mysql_fetch_array($result, MYSQL_NUM);
mysql_fetch_array($result) -> mysql_fetch_array($result, MYSQL_BOTH);
Run Code Online (Sandbox Code Playgroud)