Create XML with MySQL and PHP

Source: Internet
Author: User
Tags mysql functions
& Nbsp; xml is used as the data exchange standard. Both java and. NET support XML, but developers on other platforms do not have to worry about being abandoned by this technology. Php is a popular script environment that supports XML. & Nbsp; in this article, I will introduce you to the process of using open source tools such as PHP and MySQL to process XML. I will execute a MySQL Query and format the data obtained from the query as XML. xml is used as the data exchange standard. Both java and. NET support XML, but developers on other platforms do not have to worry about being abandoned by this technology. Php is a popular script environment that supports XML.

In this article, I will introduce you to the process of using open source tools such as PHP and MySQL to process XML. I will explain how to execute a MySQL Query and format the data obtained from the query as XML. Finally, before writing code, I will introduce how to write XML into a file and detect system settings.

To run the code provided in this article, you need to run PHP and MySQL. to make full use of these examples, you also need to know the host name, user name, and password. The format of MySQL database in the example is as follows. Let's see how to use PHP to connect to the database.

Figure



Use PHP to establish a database connection

The following PHP script establishes a connection to the database and executes a query:

$ Db_name = "xrandomusa_4 ";
$ Connection = mysql_connect ("MySQL.somewhere.com", "username", "passWord") or die ("cocould not connect .");
$ Table_name = 'page ';

Query MySQL

After the MySQL connection is established, you must use the connection to establish the current database. The following code is used to complete this task:

$ Db = mysql_select_db ($ dbName, $ link );

Now, use an SQL statement to select$ Table_nameAll rows in:
$ Query = "select * from". $ table_name;

If necessary, you can add attributes later. Run the following query:
$ Result = mysql_query ($ query, $ connection) or die ("cocould not complete database query ");
$ Num = mysql_num_rows ($ result );

You can use the PHP.net website to obtain reference information about all MySQL functions.

Create and write XML

Now you are ready to create a new XML document. There are many ways to do this, but I thinkListThe methods used can satisfy the vast majority of purposes.

Now we will detail this process. VariableNumIndicates whether or not the data row you query appears.Mysql_num_rowsFunctions are measurable. The variable $ file contains a pointer pointing to the file object generated when PHP successfully searches results. xml in the file system. If result. xml is found, your php file object and the specified file are created, and their attributes are writable. Now you can print the content of a variable to the created File (because your directory has been set to allow PHP to write files, it is no problem to do so ).

Note: from the security perspective, it is silly to do so in actual application development. To ensure that you can safely implement the concepts involved in this article, you should provide a full path for the file you want to open and write, and ensure that the file is under your Web root directory.

ThenMysql_fetch_arrayFunction to query variables$ ResultConvert it into an array and cycle it by its keyword. IfPgaeTitleIn the column returned by the query, return values for each row are directed to the string variable.$ _ XmlWrite some text.

Note that the operator ". =" is used to concatenate strings in XML format as numerical values.$ Row. When the loop ends, the root XML node is printed to the variable.$ _ XmlAll the variables inFwriteThe function is written into the file. xml file.

Now, a link is displayed on the screen. Make sure that this link points to the path of your XML file, otherwise you will not be able to see this formatted XML (this file is generated when PHP is used to process MySQL queries ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.