maxbnet - 2006-02-09

Thank you for your work.
But Imho parse_query2xml is better in this way (sorry for english)

if I call the function $query  = "Select Field1,Field2 from Table

I obtain
<table>
<row>
<field1>value</field1>
<field2>value</field2>
</row>
</table>

and i don't obtain 10 field empty with php's notice.

thanks a lot bye.Max

function parse_query2xml ($db, $query, $file)
       {

           // Check connection state
           if (! $this->db_state )
           {
               //Alert to console - Return error
               printf ("Not connected to database server.\n");
               return (-1);
           }

           //Open XML document - Check access.
           if (! $fout    =fopen($file, "w") )
           {
               // Alert to console - Return error
               printf ("Unable to open file %s for XML output.\n", $file);
               return (-1);
           }

           // Write XML/db2xml info
           fputs ($fout, "<?xml version=\&quot;1.0\&quot;?>\n");
           //fputs ($fout, "\t<db2xml action=\&quot;query2xml\&quot; target=\&quot;$query\&quot; />\n");

           // Select database - check
           if (! mysql_select_db($db, $this->db_conn))
           {
               // Alert to console - Return error
               printf ("Unable to use database %s.\n", $db);
               return (-1);
           }

           //Parse out table name
           $patch_query =str_replace(",",", ",$query);
           $vs    =split(" ", $patch_query);

           for ($ct=0;$ct<sizeof($vs);$ct++)
           {
               if (strtolower($vs[$ct])=="from")
               {
                   $table    =$vs[$ct+1];
               }
           }
                       //Parse out field name
           $arr_fields = array();
           $interruttore = false;
           for ($ct=0;$ct<sizeof($vs);$ct++)
           {
               $vs[$ct] = str_replace(",","",$vs[$ct]);
               if(strtolower($vs[$ct])=="select")
                   $interruttore = true;
               if(strtolower($vs[$ct])=="from")
                   $interruttore = false;
               if($interruttore == true && strtolower($vs[$ct])!="select")
                   array_push($arr_fields,$vs[$ct]);
           }
                       // Query all elements from table - check
           if (! $result    =mysql_query ($query))
           {
               printf ("Invalid query '%s'.\n", $query);
               return (-1);
           }

           // Write table name tag
           fputs ($fout, "\t\t<$table>\n");

           // List of field names for tag data

           // Return table fields - # of fields
           $tbl_fields = mysql_list_fields($db, $table, $this->db_conn);    //Store table fields
           $num_fields = mysql_num_fields($tbl_fields);    // Store number of fields
                       // Num_fields Patchato
           $num_fields = sizeof($arr_fields);
           // Loop through rows
           while ($row=mysql_fetch_array ($result))    // Store table row info
           {
               //Write $ctable-row tag
               fputs ($fout, "\t\t\t<$table-row>\n");

               //Loop through fields
               for ( $ct =0; $ct < $num_fields; $ct++ )    //Traverse field names
               {
                   //$cell = null;
                   //Parse field name
                   $fld_name    =mysql_field_name($tbl_fields, $ct);    // Store field name
                   $fld_type    =mysql_field_type($tbl_fields, $ct);    // Store field type
                   //if(isset($row[$ct]))
                       $cell    =$row[$ct];    //Store cell data
                                          
                   //Write field data
                   fputs ($fout, "\t\t\t\t<$fld_name type=\&quot;$fld_type\&quot;>".htmlspecialchars(addslashes($cell))."</$fld_name>\n");
               }

               //End $ctable-row tag
               fputs ($fout, "\t\t\t</$table-row>\n");

           }

           // End $table tag
           fputs ($fout, "\t\t</$table>\n");

           // Free result
           mysql_free_result ($result);

           // Close XML document
           fclose ($fout);

       }

   }

--
Massimiliano Balestrieri
info@maxb.net
http://www.maxb.net