Results 1 to 2 of 2

Thread: MYSQL to XML using PHP

  1. #1
    Join Date
    Aug 2013

    MYSQL to XML using PHP

    Hello Sir.
    I am trying to convert the MYSQL data into XML using the following code :

    // database constants
    // make sure the information is correct
    define("DB_SERVER", "localhost");
    define("DB_USER", "root");
    define("DB_PASS", "");
    define("DB_NAME", "walid");
    // connection to the database
    $dbhandle = mysql_connect(DB_SERVER, DB_USER, DB_PASS)
    or die("Unable to connect to MySQL");

    // select a database to work with
    $selected = mysql_select_db(DB_NAME, $dbhandle)
    or die("Could not select the database");

    // return all available tables
    $result_tbl = mysql_query( "SHOW TABLES FROM ".DB_NAME, $dbhandle );

    $tables = array();
    while ($row = mysql_fetch_row($result_tbl)) {
    $tables[] = $row[0];

    $output = "<?xml version=\"1.0\" ?>\n";
    $output .="<tables>\n";
    // iterate over each table and return the fields for each table
    foreach ( $tables as $table ) {
    $output .= "<table name=\"$table\">\n";
    $result_fld = mysql_query( "SHOW FIELDS FROM ".$table, $dbhandle );

    while( $row1 = mysql_fetch_row($result_fld) ) {
    $output .= "<field name=\"$row1[0]\" type=\"$row1[1]\"";
    $output .= ($row1[3] == "PRI") ? " primary_key=\"yes\" />\n" : " />\n";

    $output .= "</table>\n";
    $output .="</tables>\n";

    // tell the browser what kind of file is come in
    header("Content-type: xml");

    echo $output;

    $handle = fopen("MyXML.xml", "w") or die("unable to create file");
    file_put_contents("MyXML.xml", $output);

    // close the connection

    Sir , I have 2 questions :

    how can i update the code in order to show the content of the data in MYSQL ? This code is only showing the table and fields name and the fields types.

    How i can update the code in order to show the foreign key and relation between tables ?
    It is Showing only the primary key .

    Help Please.
    Thank you

  2. #2
    Join Date
    Jul 2010
    I believe that xml is an optional output format for the mysqldump (a common program).

    The mysqldump client is a backup program originally written by Igor Romanenko. It can be used to dump a database or a collection of databases for backup or transfer to another SQL server (not necessarily a MySQL server). The dump typically contains SQL statements to create the table, populate it, or both. However, mysqldump can also be used to generate files in CSV, other delimited text, or XML format.


    mysqldump -u root -B walid --xml > db_dump.xml
    I use (, ; : -) as I please- instead of learning the English language specification: I decided to learn Scheme and Java;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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