SQL数据库查询的输出错误

时间:2009-08-18 13:44:55

标签: php sql database

我有以下内容:

<?php
$result = mysql_query("SELECT `category_id` FROM `categories` WHERE `category_parent_id` = '80'");
while ($row = mysql_fetch_array($result)){
$childrows [] = $row['category_id'];
$clean = array_unique($childrows);
$category_string = implode(",",$clean);
echo $category_string;
?>

这输出:

  

4747,4847,48,6347,48,63,6447,48,63,64,6847,48,63,64,68,69

我无法弄清楚为什么我有重复项,有些人会丢失逗号。

请帮忙!

2 个答案:

答案 0 :(得分:4)

每次循环时都会回显,并且没有换行符。

如果您有换行符,它将如下所示:

47
47,48
47,48,63
47,48,63,64
47,48,63,64,68
47,48,63,64,68,69

现在更有意义吗?

答案 1 :(得分:2)

您的代码将无法编译,因为您没有关闭花括号,但它实际上输出了这个:

47
47,48
47,48,63
47,48,63,64
47,48,63,64,68
47,48,63,64,68,69

没有换行符。

使用此:

<?php
$result = mysql_query("SELECT DISTINCT `category_id` FROM `categories` WHERE `category_parent_id` = '80'");
while ($row = mysql_fetch_array($result))
    $childrows [] = $row['category_id'];
echo implode(",",$childrows) . "\n";
?>