A PHP XML class for MySQL_PHP Tutorial

WBOY
Release: 2016-07-21 16:08:46
Original
860 people have browsed it

我承认我不是PHP的领导者。然而,在看了一些PHP的信息之后,我认为有一些功能需要添加到其中来处理数据库连接和整合XML。要做到这一点,我想我可以创建一个处理连接MySQL和使用PHP中的domxml功能来提供XML输出的类。然后我就可以在PHP脚本的任何地方声明这个类并且在需要使用它的时候可以提供XML功能。

 
我假设人们使用PHP是原因是他的标价:免费。MySQL为需要向系统中增加数据库功能的开发人员提供一个免费的数据库解决方案。这些解决方案的缺点是在设置和管理的时候有些复杂。

我在这篇文章中使用的PHP版本是PHP 4.3.4 for Win32,可以从The PHP Group下载。MySQL的版本是MySQL 4.0.16 for Win32,可以从MySQL.com得到。MySQL的安装很容易——只要简单地按照其指令来就可以了。PHP稍微有一点复杂。

在PHP的下载页面有两个文件:一个ZIP文件和一个安装文件。因为我们需要添加ZIP文件中的扩展,所以这两个文件都要下载。下面是下载之后的所要做的一个简单步骤:

1. 使用安装文件安装PHP。

2. 解压iconv.dll,将其放到Windows的系统文件夹中。

3. PHP安装目录下创建一个目录(默认为C:\PHP)“extensions”。

4. 解压php_domxml.dll文件到这个目录。

5.  在Windows文件夹下找到php.ini文件,然后使用记事本或其它文本编辑器打开。在这个文件中找到“extensions_dir=”,然后将其值修改为第3步设置的扩展文件夹的完整路径。

6. 找到“;extension=php_domxml.dll”,删除本行开头的分号。

7.重新启动Web服务器。

然后在你的Web目录下使用下面的代码创建一个PHP页面“test.php”。(这段代码在运行IIS 5.0的Windows 2000 SP3能够正常运行。)

$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("");

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

                                                                                                                                                                                                                                                                  been have—

users". Also, you will need to create a user to access the data on the test database. For the steps to create databases, tables, etc., you can 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: the 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 a 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. Use the domxml_open_mem() function to create a new DOMDocument object. 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 result set's table to the DOMDocument document element. Then add an element to the row element for each field named fieldname. Finally, a text node is added to each field node with the node's value being 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 called. The return value of this method is returned to the client. The domxml functions comply with the DOM specification except for the PHP function naming convention.

If you need more information about the DOM specification, you can visit the W3C's site. More information about domxml can be found from The PHP Group, where you can download documents in different formats.

----------------------------------------- ---------------------------------------

The author of this article: Phillip Perkins is Ajilon Consulting signee. His experience ranges from machine control and client/server to intranet applications.



http://www.bkjia.com/PHPjc/314726.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/314726.htmlTechArticleI admit that I am not a PHP leader. However, after looking at some information on PHP, I think there are some features that need to be added to it to handle database connections and integrate XML. To do this...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template