Tutorials to .com

Tutorials to .com » Php » Xml » Using MySQL and PHP to create XML

Using MySQL and PHP to create XML

Print View , by: iSee ,Total views: 10 ,Word Count: 920 ,Date: Thu, 16 Apr 2009 Time: 1:50 AM

xml is a data exchange standards as a result. java and. NET support for all internal XML, but the development of other platforms also do not have to worry that they have been abandoned by the technology. php is a popular scripting environment to support XML.

In this article, I will introduce you to use, such as PHP and mysql open-source tools such as xml to deal with the process. I will be the implementation of a MySQL query and receive data from the query for the xml format as an example to illustrate this. Finally, immerse ourselves in the preparation of the code at the beginning, I will introduce how to write XML documents and to detect system settings.

In order to run the code given in this article, you need to have to run PHP and MySQL, and in order to take full advantage of these examples, you need to know the host name, user name and password. Example of the format of the MySQL database as shown in Figure A. Let us look at how to use PHP to connect to the database.

Figure A

Establish database connection using PHP

The following PHP script to establish the connection to the database and the implementation of a query:

<? php
$ db_name = "xrandomusa_4";
$ connection = mysql_connect ( "MySQL.somewhere.com", "username", "password") or die ( "Could not connect.");
$ table_name = 'pages';

MySQL Query

In 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, with a SQL statement to select all of the $ table_name line:
$ query = "select * from". $ table_name;

If necessary, you can add attributes later. Now, execute the query as follows:
$ result = mysql_query ($ query, $ connection) or die ( "Could not complete database query");
$ num = mysql_num_rows ($ result);

You can get on the PHP.net site to reference all MySQL functions.

And the preparation of the establishment of XML

Now, you should have been done to create a new XML document prepared. There are many methods, but I think the list of A in the method used to meet the vast majority of the purpose.

Now we detail the process. Num variables that you query the data line there or not, its function is mysql_num_rows of MySQL can be measured. Variable $ file contains a pointer, the pointer point to PHP in the file system in search of success to produce a document when results.xml object. If you find result.xml, the PHP files to create your object and the specified file, and the attribute is writable. Now you can print the contents of a variable to create a file (as a result of your directory has been set up to allow PHP to write files, so there is no problem to do so).

Note that from a security perspective, in the actual application development to do so is very foolish. In order to ensure that you can achieve security by involving the concept of this article, you should be open and you want to write full path to the document and ensure that your files are located under the Web root directory.

And then, PHP's mysql_fetch_array function of the variable $ result query into an array, and its cycle of keywords. If pgaeTitle returned in the query field, the return value of each line to the string variable $ _xml write some text.

".="( Operator attention to the XML format is used as a numerical string to splicing) to read $ row. When the end of the cycle, the root XML node to the variable $ _xml print, and all of the variables using PHP's fwrite function to write the document file.xml.

Now, the screen shows a link. Ensure that it links to your XML file path, otherwise you can not see this format XML (the document processing MySQL query using PHP generated).

Php XML Application Articles

Can't Find What You're Looking For?

Rating: Not yet rated


No comments posted.