简体   繁体   English

在PHP中从MySQL查询创建时的XML结构(另一种)

[英]XML structure while creating from MySQL query in PHP (another one)

I recently asked this question, XML structure while creating from MySQL query in PHP . 我最近问了这个问题,即从PHP中的MySQL查询创建XML结构 I got the answer I needed but now I have a similar case that is missing just one thing. 我得到了所需的答案,但是现在我有一个类似的案例,仅缺少一件事。

I duplicated the code and changed as needed but the category rows are not working. 我复制了代码并根据需要进行了更改,但类别行不起作用。

Here is my code 这是我的代码

$xml2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$xml2 .= "\r\n";
$xml2 .= "<resources>";
$xml2 .= "<version>1</version>";
$xml2 .= "\r\n";

//select all items in table
$sql2 = "SELECT distinct category, name FROM user where user = '$user' and category is not null order by category";

$result2 = mysql_query($sql2);
if (!$result2) {
    die('Invalid query: ' . mysql_error());
}

if(mysql_num_rows($result2)>0){

    while($row2 = mysql_fetch_assoc($result2)){

        if(!isset($previousRow2) || !isset($previousRow2["category"]) || $previousRow2["category"] != $row2["category"])
        {
            $xml2 .= "\r\n";
            $xml2 .= "<category title=\"" . $row["category"] . "\" />\r\n";
        }
        $xml2 .= "<item drawable=\"";

        $xml2 .= $row2["name"];

        $xml2 .= "\" />";
        $xml2 .= "\r\n";


        $previousRow2 = $row2;
    }
}

$xml2 .= "</resources>";

This is outputting this 这是输出

<category title="" />
<item drawable="bigdx_clean" />

<category title="" />
<item drawable="bluetooth" />
<item drawable="bluetooth_audio" />
<item drawable="browser" />
<item drawable="calculator" />
<item drawable="calendar" />
<item drawable="call_history" />
<item drawable="camera" />

The titles are blank though. 虽然标题是空白的。

I'm using the same concept of the working code from other question. 我正在使用其他问题中工作代码的相同概念。 It's a different query so I must have something wrong with it? 这是一个不同的查询,所以我一定有什么问题吗?

  $xml2 .= "<category title=\"" . $row["category"] . "\" />\r\n";

You are accessing $row["category"] , but this variable is not defined anywhere. 您正在访问$row["category"] ,但未在任何地方定义此变量。 Did you mean $row2 ? 您是说$row2吗?

For what it's worth, $row2 isn't a very descriptive name for a variable. 就其价值而言, $row2不是变量的描述性名称。 If you are looking for another variable name because $row is taken, perhaps you need to move this into another function, where $row will just be local? 如果由于采用$row而正在寻找另一个变量名,也许您需要将其移到另一个函数中,其中$row只是局部的?

If you are (inadvertently) accessing variables that do not exist, it is likely that your on-screen errors are disabled in your local PHP configuration. 如果您(无意间)访问了不存在的变量,则很可能在本地PHP配置中禁用了屏幕错误。 It is a good idea to turn these on to aid your development process - you'll find that setting in your php.ini configuration. 开启这些设置以帮助您的开发过程是一个好主意-您将在php.ini配置中找到该设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM