Tutorials to .com

Tutorials to .com » Php » Xml » One for the MySQL Class PHP XML

One for the MySQL Class PHP XML

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

I admit that I am not the leader in PHP. However, in reading some information on PHP, I think there are some features which need to deal with added to the database connection and integration of XML. To achieve this, I think I can handle to create a connection using mysql and php in the domxml functions to provide the type of xml output. Then I can PHP scripts anywhere in this type of statement and when to use it can provide xml functionality.

I assume that people use PHP is because of his price: free. MySQL system needed to increase the development of the database features to provide a free database solutions. The shortcomings of these solutions is to set up and manage the complexity of some of the time.

In this article I use the PHP version is PHP 4.3.4 for Win32, can be downloaded from the The PHP Group. MySQL is a version of MySQL 4.0.16 for Win32, can be obtained from MySQL.com. MySQL is very easy to install?? Simple in accordance with its instructions to it. PHP a little bit complicated.

The download page in PHP there are two documents: a ZIP file and an installation file. Because we need to add ZIP file extension, so it should download the two documents. Below is what we have to do after downloading a simple steps:

1. The use of the installation file to install PHP.

2. Decompress iconv.dll, and put it up on the system Windows folder.

3. PHP installation directory create a directory (default is C: PHP) "extensions".

4. Php_domxml.dll extract the files to this directory.

5. In the Windows folder find the php.ini file, and then use Notepad or other text editor to open. Found in the document "extensions_dir =", and then modify the value set for Step 3 of the expansion of the full path of the folder.

6. To find "; extension = php_domxml.dll", delete the semicolon at the beginning of the Bank.

7. Restart the Web server.

And then in your Web directory using the following code to create a PHP page "test.php". (This code is running IIS 5.0 in Windows 2000 sp3 to normal operation.)

<? 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);


Ask you in this case, there is a MySQL database "test", which has a table "users". In addition, you also need to access data on the test database to create a user. Create database, table, such as MySQL can see the steps of the document.

If you analyze the code, you will understand what I have created a class called CMySqlXML. CMySqlXML constructor to accept four parameters: MySQL host name, a legitimate user name, a password and a database name. Constructor parameter setting using the four categories of host, user, password and db member variable.

Provided such a method is the only run_sql_return_xml (). It to accept a SQL query string parameters. When the implementation of this method, it creates a connection to the MySQL database and select database. Query string was executed, the result stored in the variable $ result. The use of domxml_open_mem () function to create a new DOMDocument object. Then, the code to start the cycle all records of the results of focus. For each record, add a table with the result set rows of the same name DOMDocument document element to element. For each field and then add an element to the line element, the element known as field names. Finally, a text node is added to each field node, the node value of the value of the field.

All rows in the cycle after the release of the results of the code set and close the connection. DOMDocument xml generated from the return function.

In the PHP page you will see the beginning of CMySqlXML object is instantiated, run_sql_return_xml () method is called. This approach has been to return to the return value to the customer. domxml functions in addition to PHP function naming convention other than comply with the DOM specification.

If you need more information on the DOM specification, you can visit the W3C site. Domxml and more information can be found from The PHP Group, where you can download the document in different formats.

Php XML Application Articles

Can't Find What You're Looking For?

Rating: Not yet rated


No comments posted.