SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2003
    Location
    London
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pulling data from db and formatting as XML

    Hi there I'm triying to write a little function to pull data from a table and format it as XML it should be pretty straight forward but is not working for some reason....

    this is what I've got
    PHP Code:
    <?php
    require_once("db.php");
    echo <<<END
    <?xml version="1.0" encoding="ISO-8859-1"?> // this is the first problem , how do I scape the ?> so php continues executing??
    END;

    #TAG - PRINT XML
    function TAG_printXML(){
    $db db_connect(); // contained on db.php
    $query "SELECT * FROM table";
    // Execute the query to get the results         
    $result mysql_query($query) or die("Query Failed");

    // Iterate throught the result rows
    $recordcount mysql_numrows($result);
    while (
    $line mysql_fetch_array($resultMYSQL_ASSOC)) {
      
    $i=0;
      foreach (
    $line as $col_value) {
        
    $fieldname=mysql_field_name($result,$i);
        if(
    $i==0){
          print 
    "<record id='$col_value'>";
            print (
    "<rowitem");
            }
        else
            print (
    $fieldname='$col_value'");
            
    $i++;
        }
      print (
    "/></record>");

    print(
    " </xml>");

    }




    TAG_printXML();




    ?>
    I know I must be doing something wrong but I can't quite see what's the problem now...

    Thanks for your help.
    Last edited by perate; Oct 4, 2003 at 08:13.

  2. #2
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by perate
    PHP Code:
    <?php
    require_once("db.php");
    echo <<<END
    <?xml version="1.0" encoding="ISO-8859-1"?> // this is the first problem , how do I scape the ?> so php continues executing??
    END;
    XHTML doesn't explicitly require this tag, but a better solution would be to disable short_tags via .htaccess

    .htaccess
    Code:
    php_flag short_open_tags off
    Of course, this only works with Apache, so for IIS or other webserver you would need access to php.ini

    Edit:


    Not sure how I got onto XHTML... but yes, I believe XML requires this...
    Last edited by lieut_data; Oct 4, 2003 at 08:52.
    My name is Steve, and I'm a super-villian.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2003
    Location
    London
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the info but.. what does disabling short_tags do? I don't need to declare the xml any more??

    Even if I ommit the xml declaration it still does not work for me thanks for your help.

  4. #4
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by perate
    Thanks for the info but.. what does disabling short_tags do? I don't need to declare the xml any more??

    Even if I ommit the xml declaration it still does not work for me thanks for your help.
    Oops, forgot to post the rest of the problem (still testing it) -- disabling the short tags prevents PHP from looking for <?, instead looking exclusively for <?php -- then the XML Declaration won't throw any parse errors (and you can keep it)

    Edit:


    First off -- I notice there is no starting <xml> tag.

    Second -- what is the problem you are running into?
    My name is Steve, and I'm a super-villian.

  5. #5
    o_O O_o BlueFire2k5's Avatar
    Join Date
    Mar 2003
    Location
    Sioux Falls, SD
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHP Manual
    short_open_tag boolean

    Tells whether the short form (<? ?>) of PHP's open tag should be allowed. If you want to use PHP in combination with XML, you can disable this option in order to use <?xml ?> inline. Otherwise, you can print it with PHP, for example: <?php echo '<?xml version="1.0"'; ?>. Also if disabled, you must use the long form of the PHP open tag (<?php ?>).

    Note: This directive also affects the shorthand <?=, which is identical to <? echo. Use of this shortcut requires short_open_tag to be on.
    Edit:

    Too late. >_>

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2003
    Location
    London
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you both for the info on short_open_tag I've checked it on php.ini and in enhabled..

    The problem that I'm running into is that it doesn't print anything... It's even scaping the

    PHP Code:

    print(" </xml>" ); 
    at the end... so that means is not going through it...

    I've even put this to test

    PHP Code:

    while ($row mysql_fetch_array($resultMYSQL_ASSOC)) {

    echo 
    $row['id']; 
    and it doesn't print the id the whole thing is a small cms and I'm accessing the data from other scripts so I know is there and everything work fine but this one...

    Thanks for your help again.

    P.S. I've included the opening <xml> tag now...

  7. #7
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, just after

    PHP Code:
    // Iterate throught the result rows
    $recordcount mysql_numrows($result); 
    Add
    PHP Code:
    echo "Record Count: $recordcount"
    And see if you get any output.

    You may also want to add the following at the very top of the script:

    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    My name is Steve, and I'm a super-villian.

  8. #8
    SitePoint Enthusiast
    Join Date
    Aug 2003
    Location
    London
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    it's woking

    ok this is quite embarrasing .. I got it working and I think Apart from minor mistakes the main issue (I must confess )was that I wasn't pointing to the right include file...

    the final working code for the record...

    PHP Code:

    require_once("config.php");
    #################################################################


    echo <<<END
    <?xml version="1.0" encoding="ISO-8859-1"?>\n
    END;


    #TAG - PRINT XML
    ############################
    function TAG_printXML(){
    echo "<xml>\n";
    $db = db_connect();
    $query = "SELECT * FROM table";
    // Execute the query to get the results         
    $result = mysql_query($query) or die("SQL Error (TAG_printXML()): " . mysql_error() . " in Query: $query");
    // Iterate throught the result rows
    $recordcount = mysql_numrows($result);
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "<item>\n";
    while(list($key,$val) = each($row) ) { 
    echo "<$key>$val</$key>\n"; 

    echo "</item>\n";

    echo "\n</xml>";
    }



    TAG_printXML();




    ?>
    thanks again for your help....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •