duanditang2916 2014-08-26 22:36
浏览 30
已采纳

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

I recently asked this question, XML structure while creating from MySQL query in PHP. 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 .= "
";
$xml2 .= "<resources>";
$xml2 .= "<version>1</version>";
$xml2 .= "
";

//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 .= "
";
            $xml2 .= "<category title=\"" . $row["category"] . "\" />
";
        }
        $xml2 .= "<item drawable=\"";

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

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


        $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"] . "\" />
";
  • 写回答

1条回答 默认 最新

  • dtkl55257 2014-08-26 22:44
    关注

    You are accessing $row["category"], but this variable is not defined anywhere. Did you mean $row2?

    For what it's worth, $row2 isn't a very descriptive name for a variable. 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?

    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. It is a good idea to turn these on to aid your development process - you'll find that setting in your php.ini configuration.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据