0

I have been trying to connect a database to generate XML File through PHP code, but I still have a problem on this code. Can somebody help me correcting this code? Thanks in advance.

<?php
        @ $db = new mysqli('localhost','622Std1', '622StdDB1', '622stddb1');
        if (mysqli_connect_errno()) {
        echo 'Error: Could not connect to database';
        exit;
        }

        $query = "select * from bookmark";
        $result = $db->query($query);

        //$bookmarks[]=array('username'=>'user1','bm_URL'=>'http://msn.com');
        //$bookmarks[]=array('username'=>'user2','bm_URL'=>'http://php.net');
        //$bookmarks[]=array('username'=>'user3','bm_URL'=>'http://google.com');
        //$bookmarks[]=array('username'=>'user4','bm_URL'=>'http://msn.com');

        $num_results=mysqli_num_rows($result);

        for($i=0;$i<$num_results;$i++)
        {

        $row = $result->fetch_assoc();
        echo "<br/><br/>Username:";
        echo stripslashes($row['username']);
        echo "<br/>URL:";
        echo stripslashes($row['bm_URL']);

        $document=new DOMDocument();
        $document->formatOutput=true;


        $rootElement=$document->createElement("bookmarks");
        $new_node=$document->appendChild($rootElement);

            foreach($bookmarks as $key => $bookmarks)
            {
                $itemElement=$document->createElement("bookmark");

                $Name=$document->createElement("username");
                $Name->appendChild($document->createTextNode($row['username']));
                $itemElement->appendChild($Name);

                $BookMarkurl=$document->createElement("bm_URL");
                $BookMarkurl->appendChild($document->createTextNode($row['bm_URL']));
                $itemElement->appendChild($BookMarkurl);

                $rootElement->appendChild($itemElement);

            }

        $document->save("GeneratedXMLFile.xml");
        echo "Confirmed! the XML file is generated";
        }
      $db->close();
    ?>

Edited by Reverend Jim: Fixed formatting

2
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by alafift
0

Could you be more specific? What is going wrong and where? Does the code work (it doesn't contain php errors) but doesn't do what you want or is the code not working at all?

0

Thanks, the code could not show any outputs, but I think I got it working now. It only needs to be connected with the database in the lab to see the results and the XML generated file. So, I only used the the forloop to fetch the rows from the the database table "bookmark" instead of using the associative array which is there no need for. It may work this way!

<?php
        @ $db = new mysqli('localhost','622Std1', '622StdDB1', '622stddb1');
        if (mysqli_connect_errno()) {
        echo 'Error: Could not connect to database';
        exit;
        }
        $query = "select * from bookmark";
        $result = $db->query($query);

        //$bookmarks[]=array('username'=>'user1','bm_URL'=>'http://msn.com');
        //$bookmarks[]=array('username'=>'user2','bm_URL'=>'http://php.net');
        //$bookmarks[]=array('username'=>'user3','bm_URL'=>'http://google.com');
        //$bookmarks[]=array('username'=>'user4','bm_URL'=>'http://msn.com');

        $num_results = $result->num_rows ;

        if($num_result > 0)
        {

            for($i=0;$i<$num_results;$i++)
            {

            $row = $result->fetch_assoc();
            echo "Username:<br/>";
            echo $row['username'];
            echo "Bookmark_URL:<br/>";
            echo $row['bm_URL'];

            $document=new DOMDocument();
            $document->formatOutput=true;


            $rootElement=$document->createElement("bookmarks");
            $new_node=$document->appendChild($rootElement);

            //foreach($bookmarks as $key => $bookmarks)
            //{
                $itemElement=$document->createElement("bookmark");

                $User_name=$document->createElement("username");
                $User_name->appendChild($document->createTextNode($row['username']));
                $itemElement->appendChild($User_name);

                $BookMarkurl=$document->createElement("bm_URL");
                $BookMarkurl->appendChild($document->createTextNode($row['bm_URL']));
                $itemElement->appendChild($BookMarkurl);

                $rootElement->appendChild($itemElement);

                //}

        $document->save("GeneratedXMLFile.xml");
        echo "Confirmed! the XML file is generated";
        }
      }
    else{
    echo "No records found"."You need to enter records in the bookmark table";
    }
      $db->close();

Edited by Reverend Jim: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.