A php xml class for MySQL

Source: Internet
Author: User
Tags mysql host php download windows 5

I admit that I am not the leader in PHP. However, after reading some PHP information, I think some functions need to be added to it to process database connections and integrate XML. To do this, I think I can create a class to process MySQL connections and use the domxml function in PHP to provide XML output. Then I can declare this class anywhere in the PHP script and provide the XML function when I need it.

 
I suppose people use PHP because of his price: free. MySQL provides a free database solution for developers who need to add database functions to the system. The disadvantage of these solutions is that they are complicated in setup and management.

The PHP version I used in this article is PHP 4.3.4 for Win32, which can be downloaded from The PHP Group. The MySQL version is MySQL 4.0.16 for Win32, which can be obtained from MySQL.com. MySQL installation is easy-simply follow the instructions. PHP is a little complicated.

The PHP download page contains two files: a ZIP file and an installation file. Because we need to add extensions in the ZIP file, both files must be downloaded. The following is a simple step:

1. Use the installation file to install PHP.

2. Decompress iconv. dll and place it in the Windows system folder.

3. Create a directory under the PHP installation directory (default: C: \ PHP) "extensions ".

4. decompress the php_domxml.dll file to this directory.

5. Find the php. ini file in the Windows folder and open it in notepad or another text editor. Find "extensions_dir =" in this file and change its value to the full path of the extension folder set in step 1.

6. Find "; extension = php_domxml.dll" and delete the semicolon starting with this line.

7. Restart the Web server.

Then, use the following code in your Web directory to create a PHP page named "test. php ". (This code runs properly on Windows 5.0 SP3 running IIS 2000 .)

<? Php

$ Myxml = new CMySqlXML ("localhost", "test_user", "password", "test ");

Echo $ myxml-> run_ SQL _return_xml ("SELECT * FROM users ");

ClassCMySqlXML {

Var $ host;

Var $ user;

Var $ password;

Var $ db;

FunctionCMySqlXML ($ host, $ user, $ password, $ db ){

$ This-> host = $ host;

$ This-> user = $ user;

$ This-> password = $ password;

$ This-> db = $ db;

}

Functionrun_ SQL _return_xml ($ SQL _string ){

$ Connection = mysql_connect ($ this-> host, $ this-> user, $ this-> password,

$ This-> db );

Mysql_select_db ($ this-> db );

$ Result = mysql_query ($ SQL _string );

$ Doc = domxml_open_mem ("<root/> ");

While ($ row = mysql_fetch_array ($ result, MYSQL_ASSOC )){

$ Num_fields = mysql_num_fields ($ result );

$ Row_element = $ doc-> create_element (mysql_field_table ($ result, 0 ));

$ Doc_root = $ doc-> document_element ();

$ Row_element = $ doc_root-> append_child ($ row_element );

For ($ I = 0; $ I <$ num_fields; $ I ++ ){

$ Field_name = mysql_field_name ($ result, $ I );

$ Col_element = $ doc-> create_element ($ field_name );

$ Col_element = $ row_element-> append_child ($ col_element );

$ Text_node = $ doc-> create_text_node ($ row [$ field_name]);

$ Col_element-> append_child ($ text_node );

}

}

Mysql_free_result ($ result );

Mysql_close ($ connection );

Return $ doc-> dump_mem (false );

}

}
This example requires that you have a database "test" in MySQL, with a table "users ". You also need to create a user for accessing the data in the test database. To create a database or table, you can view the MySQL documentation.


If you analyze the code, you will understand that I have created a class named CMySqlXML. The CMySqlXML constructor accepts four parameters: MySQL host name, a valid user name, a password, and a database name. The constructor uses these four parameters to set the host, user, password, and db member variables of the class.

The only method provided by this class is run_ SQL _return_xml (). It accepts an SQL query string parameter. When this method is executed, it creates a connection to the MySQL database and selects the database. The query string is executed and the result is stored in the variable $ result. Use the domxml_open_mem () function to create a new DOMDocument object. Then, the Code starts to loop all records in the result set. For each record, add a row element with the same name as the result set table to the DOMDocument document element. Add an element to the Row Element for each field. The element name is the field name. Finally, a text node is added to each field node. The Node value is the value of this field.

After looping all rows, the Code releases the result set and closes the connection. The generated DOMDocument XML is returned from the function.

At the beginning of the PHP page, you will see that the CMySqlXML object is instantiated and the run_ SQL _return_xml () method is called. The return value of this method is returned to the customer. The domxml function complies with DOM specifications in addition to the naming conventions of PHP functions.

For more information about DOM specifications, visit the W3C site. More domxml information can be found in The PHP Group. Here you can download documents in different formats.


--------------------------------------------------------------------------------
Author: Phillip Perkins is the signatory of Ajilon Consulting. He has rich experience, from machine control and customer/server to enterprise intranet applications.

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.