Hiya guys

I wondered if you could point me to the right direction as to what or how I should do about the following:

I have a few MySQL tables, accessing them via PHPmyAdmin 206
I use PHP 4.23

What I want to do is to use PHP via XML to get the data from MySQL database... my knowledge with XML with PHP and MySQL is zero... could someone please let me know where to start researching...

Do I have to create XML Schemas, then connect PHP to this and access the data.. I am totally lost here... Or do I have to download a few modules on either MySQL, PHP or Apache that will take care of this matter for me....

Please help, also if there are any particular books you would recommend, I have looked in Amazon and there are quite a few books...

Kind regards

Jo

    Do you mean that you want to convert MySQL data into an XML document?

    If so, you can use this code:

    <?php
    $connection = mysql_connect("localhost","user","paswd") or die("Could not connect");
    $db = mysql_select_db("database-name") or die("Could not select db");
    
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n\n";
    echo "<database>\n";
    
    // insert you table names in this array:	
    foreach (array("table1","table2","table3","table4") as $table)
    {
    	$result = mysql_query("SELECT * FROM $table");
    
    echo "\t<$table>\n";
    
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
    	echo "\t\t<row>\n";
    
    	foreach ($row as $field=>$value)
    	{
    		echo "\t\t\t<$field>$value</$field>\n";
    	}
    
    	echo "\t\t</row>\n";
    }
    
    echo "\t</$table>\n";
    }
    
    echo "</database>";
    ?>
    

    (I wrote this code ages ago; I think it works!)

      I think he's thinking that he connects to XML via PHP and received MySQL data through XML...this can't be done, since XML is not a programming language. It's data storage. You can use sara's script to convert your MySQL DB data to an XML document, and then parse it through PHP.

        Hiya

        Thank you so so much for answering my query... here is what I want to do and then you guys could point me to the right way, so I may research more..

        1) My database is in MySQL, and my scripting language is PHP... I want to connect PHP not directly to the database but through XML... at the moment my data is accessed directly form the database and no XML is used.

        2) The reason for the XML is because if someone else from another site wants to access my database then my site will be adaptable for this integration. Also if I use another language besides PHP to access the database then XML will allow it...

        I was told that XML is for holding the structure of the data and HTML was for presentation... also XML will make the transfer of data possible between 2 systems.... its this integration that I am interested in.... e.g. Expedia talks with other airline companies and all the data is passed through XML !!!

        I am using MyAdminPHP and I can export the data through XML, which is great... but what I want to do is to have a system where my PHP script will link into the database through XML...

        I hope you can help..... AGAIN HUGE THANKS

        Jo

          Like I said before - you need some sort of PHP script to initially convert the MySQL data to an XML document. From there, you can parse the XML code using PHP's builtin XML parser. Other PHP scripts can also parse the same XML code. However, when the database changes, you have to re-run the script to produce the XML file. There is no way to "link" an XML file to a MySQL database, or to connect to MySQL through XML.

            Hmm, perhaps you want to write a web service. There are numerous articles on the Web about writing web services with PHP. First check out the PHP XML-RPC and SOAP docs.

            Other articles include
            http://webservices.xml.com/pub/a/ws/2004/03/24/phpws.html and http://www.onlamp.com/pub/a/php/2003/10/30/amazon_rest.html.

            I think you may have misunderstood the use of XML. XML is not a programming language, nor does it allow you to work with databases or provide any sort of logic or control. It is a document markup language, much like HTML except that it does not provide any standard tag set.

            I was told that XML is for holding the structure of the data and HTML was for presentation

            This is because a common application is to convert an XML document to an HTML document using XSLT.

            also XML will make the transfer of data possible between 2 systems

            This is quite a good idea, but you've got it the wrong way around. Your system will go something like this:

            DATABASE --> PHP --> XML --> [NETWORK] <-- CLIENT

            not this:

            DATABASE --> XML --> PHP --> [NETWORK] <-- CLIENT

              Exactly what I was trying to express. Thanks sara! 🙂

              Edit: XML isn't really a markup language, per se, because it does not define things like bold, italic, size, etc. It just categorizes, organizes, and stores data.

                Write a Reply...