代码之家  ›  专栏  ›  技术社区  ›  The.Anti.9

奇怪的问题回音数组(php)

  •  1
  • The.Anti.9  · 技术社区  · 16 年前

    好的,我有以下代码。我正试图输出一些XML,以便可以用jquery读取它,但是无论出于什么原因,当我试图将数组中的元素添加到变量中时,它只是将变量转换为0。我在数组上做了一个打印,这是正常的。

    代码

    if ($content == "tables") {
        $result = mysql_query("show tables");
    $xml = "<tables>";
        while ($row = mysql_fetch_assoc($result)) {
            print_r($row);
            echo "<br />";
            $xml += "<table>" . $row['Tables_in_blog'] . "</table>";
        }
        //header('Content-type: text/xml');
        echo $xml;
    }
    

    产量

    Array ( [Tables_in_blog] => post )
    Array ( [Tables_in_blog] => posts )
    0
    

    有人知道为什么会这样吗?

    1 回复  |  直到 16 年前
        1
  •  11
  •   Konrad Rudolph    16 年前
    $xml += "<table>" . $row['Tables_in_blog'] . "</table>";
    

    那是罪犯。尝试连接:

    $xml .= "<table>" . $row['Tables_in_blog'] . "</table>";
    

    你误用了 += 而不是 .= 用于连接。这触发了PHP将值转换为数字(导致 0 )并添加它们。