PHP XML classes for MySQL

Source: Internet
Author: User
Tags constructor install php mysql version naming convention php download win32 zip mysql database


I assume that people are using PHP because of his price tag: free. MySQL provides a free database solution for developers who need to add database functionality to the system. The disadvantage of these solutions is that they are somewhat complex to set up and manage.



The PHP version I use in this article is the PHP 4.3.4 for Win32, which can be downloaded from the PHP group. The MySQL version is the MySQL 4.0.16 for Win32, which can be obtained from mysql.com. MySQL is easy to install-simply follow its instructions. PHP is a little bit more complicated.



In the PHP download page There are two files: a zip file and an installation file. Because we need to add extensions to the zip file, all two files are downloaded. Here's a simple step to do after downloading:



1. Install PHP using installation files;



2. Extract the Iconv.dll and put it in the Windows System folder;



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



4. Extract Php_domxml.dll files to this directory;



5. Locate the php.ini file under Windows folder, and then open it using Notepad or another text editor. Locate "extensions_dir=" in this file and modify its value to the full path of the extended folder set in step 3rd;



6. Find "; Extension=php_domxml.dll", delete the semicolon at the beginning of the bank;



7. Restart the Web server.



Then use the following code in your Web directory to create a PHP page "test." PHP. " (This code works correctly with Windows SP3, which is running IIS 5.0.) )


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 ("");
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 you to have a database "test" on MySQL with a table of "users." Also, you need to create a user for accessing the data on the test database. Steps to create a database, table, and so on to view the MySQL documentation.



If you analyze the code, you will understand that I created a class called Cmysqlxml. The Cmysqlxml constructor accepts four parameters: MySQL's hostname, a valid username, 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 one method provided by this class is Run_sql_return_xml (). It accepts an SQL query string parameter. When this method executes, 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. Creates a new DOMDocument object using the Domxml_open_mem () function. The code then starts looping through all the records in the result set. For each record, add a row element with the same name as the table of the result set to the DOMDocument document element. Then add an element to the row element for each field, and the element name is the field name. Finally, a text node is added to each field node, and the value of the node is the value of the field.



After looping through all rows, the code releases the result set and closes the connection. The resulting 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 invoked. The return value of this method is returned to the customer. The Domxml feature adheres to the DOM specification in addition to the PHP function naming convention.



If you need more information about the DOM specification, you can access the Web site of the consortium. More domxml information can be found from the PHP group, where you can download documents in different formats.


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.