Create XML with MySQL and PHP

Source: Internet
Author: User
Tags mysql functions mysql query php file php and mysql php script mysql database
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 the MySQL database in the example is shown in Figure. 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:

<? Php
$ 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 all rows in $ table_name:
$ 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 methods, but I think the methods used in listing A can satisfy the vast majority of purposes.
Now we will detail this process. The variable num indicates whether or not the queried data row appears. It is measurable for MySQL's mysql_num_rows function. 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.
Related Article

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.