In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
What is the PHP XML class for MySQL? for this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Create a class that handles connecting to the MySQL and uses the domxml feature in PHP to provide XML output. This allows you to declare the class anywhere in the PHP script and provide XML functionality when you need to use it.
Suppose people use PHP because of its price tag: free. MySQL provides a free database solution for developers who need to add database capabilities to the system. The disadvantage of these solutions is that they are complex to set up and manage.
The version of PHP I use in this article is PHP 4.3.4 for Win32, which is available from The PHP Group. The version of MySQL is MySQL 4.0.16 for Win32, which is available from MySQL.com. MySQL is easy to install-simply follow its instructions. PHP is a little more complicated.
There are two files on the download page of PHP: a ZIP file and an installation file. Because we need to add extensions in the ZIP file, both files need to be downloaded. Here is a simple step to do after downloading:
1. Install PHP using the installation file
two。 Extract the iconv.dll and put it in the system folder of Windows
3. Create a directory under the PHP installation directory (default is C:PHP) "extensions"
4. Extract the PHP_domxml.dll file to this directory
5. Locate the PHP.ini file in the Windows folder and open it using notepad or another text editor. Find "extensions_dir=" in this file and change its value to the full path of the extended folder set in step 3
6. Find "; extension=PHP_domxml.dll" and delete the semicolon at the beginning of the line
7. Restart the Web server.
Then create a PHP page "test.PHP" in your Web directory using the following code. (this code works fine on Windows 2000 SP3 running IIS 5.0.)
The following is the referenced content:
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 "test" on MySQL with a table "users". Also, you need to create a user to access the data on the test database. To create a database, table, and so on, you can view the documentation for MySQL.
If you analyze the code, you will understand that I created a class called CMySqlXML. The CMySqlXML constructor takes four parameters: the hostname of the MySQL, 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 results are stored in the variable $result. Create 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 node's value is the value of the field.
After looping through all the 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 called. The return value of this method is returned to the customer. Domxml functions follow the DOM specification except for the PHP function naming convention.
If you need more information about the DOM specification, you can visit the W3C site. More information about domxml can be found at The PHP Group, where you can download documents in different formats.
This is the answer to the question about how the PHP XML class for MySQL is shared. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.