xml creation problem

Discussion in 'PHP' started by beermaker74, Jan 29, 2007.

  1. #1
    I have this script that I am playing with to create an xml file from a mysql query

    <?php
    
     $file= fopen("results.xml", "w");
    
     $xml_output  = "<?xml version=\"1.0\"?>\n";
    $xml_output .= "<gallery>\n";
    
    for($x = 0 ; $x < mysql_num_rows($Recordset1) ; $x++){
        $totalRows_Recordset1 = mysql_fetch_assoc($Recordset1);
        $xml_output .= "\t<listings>\n";
        $xml_output .= "\t\t<houseid>" . $totalRows_Recordset1['houseid'] . "</houseid>\n";
        $xml_output .= "\t\t<username>" . $totalRows_Recordset1['username'] . "</username>\n";
    	$xml_output .= "\t\t<address>" . $totalRows_Recordset1['address'] . "</address>\n";
        $xml_output .= "\t</listings>\n";
    }
    
    $xml_output .= "</gallery>";
    
     fwrite($file, $xml_output);
    
     fclose($file);
    
     echo "XML has been written.  <a href=\"results.xml\">View the XML.</a>";
    
    ?> 
    
    PHP:
    here is the result of a query with one record
    <gallery>
    <listings>
    <houseid/>
    <username/>
    <address/>
    </listings>
    </gallery>

    the result is not listed.
    Here is the result of a query with 2 records

    <gallery>
    <listings>
    <houseid>26</houseid>
    <username>jerry</username>
    <address>55 fake ave</address>
    </listings>
    <listings>
    <houseid/>
    <username/>
    <address/>
    </listings>
    </gallery>

    there is 2 records in that query. For some reason it is only outputing anything over 1 record. yet it is displaying blank tags

    if you see my problem please let me know. I am just trying to write from a simple query now. MY real task is to write the xml from a more advanced query. So I need to figure out the simple problem first
    thanks
     
    beermaker74, Jan 29, 2007 IP
  2. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #2
    where is your sql string in this code?
     
    falcondriver, Jan 29, 2007 IP
  3. gemini181

    gemini181 Well-Known Member

    Messages:
    2,883
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    155
    #3
    I understand you are also working on a larger project.
    Please, let me know if this helps, since it's something I need to learn anyway. :)

    Create XML with MySQL and PHP

    "... I'll execute a MySQL query and format the data into well-formed XML.
    Finally, I'll explain how to write XML to a file and examine the system setup before diving into the code..."
     
    gemini181, Jan 29, 2007 IP
  4. beermaker74

    beermaker74 Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    falcon here is my sql

    mysql_select_db($database_vhtest, $vhtest);
    $query_Recordset1 = "SELECT * FROM house WHERE " . $wherClause;
    $Recordset1 = mysql_query($query_Recordset1, $vhtest) or die(mysql_error());
    $row_Recordset1 = mysql_fetch_assoc($Recordset1);
    $totalRows_Recordset1 = mysql_num_rows($Recordset1);

    all this was above the other code i posted
    I was just trying to save space
     
    beermaker74, Jan 29, 2007 IP
  5. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You are "shifting" the first row, are you aware of this?

    mysql_select_db($database_vhtest, $vhtest);
    $query_Recordset1 = "SELECT * FROM house WHERE " . $wherClause;
    $Recordset1 = mysql_query($query_Recordset1, $vhtest) or die(mysql_error());
    $row_Recordset1 = mysql_fetch_assoc($Recordset1);
    $totalRows_Recordset1 = mysql_num_rows($Recordset1);


    Also, even if I don't think that's the problem, it's a (quite) odd way of running through a mysql result - why don't you try something like this:
    while ($row = mysql_fetch_assoc($Recordset1)) {
    
    Code (markup):
    instead of
    for($x = 0 ; $x < mysql_num_rows($Recordset1) ; $x++){
        $totalRows_Recordset1 = mysql_fetch_assoc($Recordset1);
        ...
    }
    Code (markup):
    Check out http://php.net/mysql_fetch_assoc

    HTH, cheers! :)
     
    picouli, Jan 29, 2007 IP
  6. beermaker74

    beermaker74 Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    no i wasnt aware I was shifting the row

    here is my code

    <?php
    mysql_select_db($database_vhtest, $vhtest);
    $query_Recordset1 = "SELECT * FROM house WHERE " . $wherClause;
    $Recordset1 = mysql_query($query_Recordset1, $vhtest) or die(mysql_error());
    $row_Recordset1 = mysql_fetch_assoc($Recordset1);
    $totalRows_Recordset1 = mysql_num_rows($Recordset1);
    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <?php
    
     $file= fopen("results.xml", "w");
    
     $xml_output  = "<?xml version=\"1.0\"?>\n";
    $xml_output .= "<gallery>\n";
    
    for($x = 0 ; $x < mysql_num_rows($Recordset1) ; $x++){
        $totalRows_Recordset1 = mysql_fetch_assoc($Recordset1);
        $xml_output .= "\t<listings>\n";
        $xml_output .= "\t\t<houseid>" . $totalRows_Recordset1['houseid'] . "</houseid>\n";
        $xml_output .= "\t\t<username>" . $totalRows_Recordset1['username'] . "</username>\n";
    	$xml_output .= "\t\t<address>" . $totalRows_Recordset1['address'] . "</address>\n";
        $xml_output .= "\t</listings>\n";
    }
    
    $xml_output .= "</gallery>";
    
     fwrite($file, $xml_output);
    
     fclose($file);
    
     echo "XML has been written.  <a href=\"results.xml\">View the XML.</a>";
    
    ?> 
    
    </body>
    </html>
    <?php
    mysql_free_result($Recordset1);
    }
    ?>
    
    PHP:
    I tried the codeabove and the results were sporadic. ie I know I had 8 records in a query and it returned 3. anyway where am i shifting

    is it here
    $row_Recordset1 = mysql_fetch_assoc($Recordset1);

    and if it is how is it shifting. isnt it just putting allthe results of the query into the array $row_Recordset1?
    i thought i at least understood that part
    please enlighten me
    thanks

    well i took the $row_Recordset1 = mysql_fetch_assoc($Recordset1); out of the top code and it is working justlike it should.
    I still dont understand why it is shifting
     
    beermaker74, Jan 29, 2007 IP
  7. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Try this:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <?php
    
     $file= fopen("results.xml", "w");
    
     $xml_output  = "<?xml version=\"1.0\"?>\n";
    $xml_output .= "<gallery>\n";
    
    mysql_select_db($database_vhtest, $vhtest);
    $query_Recordset1 = "SELECT * FROM house WHERE " . $wherClause;
    $Recordset1 = mysql_query($query_Recordset1) or die(mysql_error());
    
    while ($totalRows_Recordset1 = mysql_fetch_assoc($Recordset1)) {
        $xml_output .= "\t<listings>\n";
        $xml_output .= "\t\t<houseid>" . $totalRows_Recordset1['houseid'] . "</houseid>\n";
        $xml_output .= "\t\t<username>" . $totalRows_Recordset1['username'] . "</username>\n";
        $xml_output .= "\t\t<address>" . $totalRows_Recordset1['address'] . "</address>\n";
        $xml_output .= "\t</listings>\n";
    }
    
    $xml_output .= "</gallery>";
    
     fwrite($file, $xml_output);
    
     fclose($file);
    
     echo "XML has been written.  <a href=\"results.xml\">View the XML.</a>";
    
    ?>
    
    </body>
    </html>
    <?php
    mysql_free_result($Recordset1);
    }
    ?>
    PHP:
    Also, what's in your $wherClause ? Where do you set this?
     
    picouli, Jan 29, 2007 IP