dongluo1853 2013-07-20 17:17
浏览 34
已采纳

输出xml数组解析php重复六次[关闭]

I have a php file in this format:

<?php
$username = "root";
$password= "";
$db = "kamus1";
mysql_connect("localhost",$username,$password) or die("koneksi ke MySQL gagal");
mysql_select_db($db) or die ("koneksi ke dataBase gagal");

$doc = new DomDocument('1.0');
$root = $doc->createElement('movies');
$root = $doc->appendChild($root);
$arr = array();

//$query=mysql_query("select id,istilah,definisi from kamus_jaringan");
$query=mysql_query("select * from kamus_jaringan");
while($get_data = mysql_fetch_array($query))
{
foreach($get_data as $fieldname=>$fieldvalue)
{

$item = $doc->createElement('movie');
$item = $root->appendChild($item);
$item ->setAttribute("id",$get_data["id"]);
$item ->setAttribute("istilah",$get_data["istilah"]);
$item ->setAttribute("definisi",$get_data["definisi"]);
}
}
echo $doc->saveXML();
$doc->save("movies.xml");
?>

when this code is executed on the browser, the output of each of the data will be repeated up to six times. for example id, will repeat six times. I want the output to each of the data, only one time. please help

  • 写回答

1条回答 默认 最新

  • dongtui9168 2013-07-20 17:21
    关注

    First, don't use mysql_* functions, they are deprecated. Second, you have two loops and you only need one, change your code to this:

    while($get_data = mysql_fetch_array($query))
    {
        $item = $doc->createElement('movie');
        $item = $root->appendChild($item);
        $item ->setAttribute("id",$get_data["id"]);
        $item ->setAttribute("istilah",$get_data["istilah"]);
        $item ->setAttribute("definisi",$get_data["definisi"]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵