开发者

PHP MySQL results return double the values

I have a page that returns some results from a SQL query to a MysQL database. For some reason, it's returning every value twice, I think because it's stored in the database both wth a numerical index, and a string index. How can I extract just the indexes and values that have a string index, and disregard those with a integer index?

The function I'm using to generate the table is below:

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;
}

A screenshot of the result: http://i.stack.imgur.com/3YXoh.png

Thanks.

edit1 - The SQL I'm using is somewhat of a placeholder for testing. It is:

$query = "SELECT * FROM politicians";

I've also tried exlici开发者_C百科tly asking for the columns, and I get the same problem.

$query = "SELECT firstname,lastname,jurisdiction,party,riding FROM politicians";


mysql_fetch_array() will by default return a hash that contains both an associative array and a regular numeric-keyed result set of your row. To fetch one or the other use mysql_fetch_row() (array) or mysql_fetch_assoc() (hash), or specify what type you want using the optional second argument to mysql_fetch_array() as explained in its man page (linked above).

These are the equivalances:

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜