国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Backend Development PHP Tutorial A PHP XML class for MySQL_PHP Tutorial

A PHP XML class for MySQL_PHP Tutorial

Jul 21, 2016 pm 04:08 PM
mysql php xml 。 no information exist I However of kind

我承認(rèn)我不是PHP的領(lǐng)導(dǎo)者。然而,在看了一些PHP的信息之后,我認(rèn)為有一些功能需要添加到其中來(lái)處理數(shù)據(jù)庫(kù)連接和整合XML。要做到這一點(diǎn),我想我可以創(chuàng)建一個(gè)處理連接MySQL和使用PHP中的domxml功能來(lái)提供XML輸出的類(lèi)。然后我就可以在PHP腳本的任何地方聲明這個(gè)類(lèi)并且在需要使用它的時(shí)候可以提供XML功能。

?
我假設(shè)人們使用PHP是原因是他的標(biāo)價(jià):免費(fèi)。MySQL為需要向系統(tǒng)中增加數(shù)據(jù)庫(kù)功能的開(kāi)發(fā)人員提供一個(gè)免費(fèi)的數(shù)據(jù)庫(kù)解決方案。這些解決方案的缺點(diǎn)是在設(shè)置和管理的時(shí)候有些復(fù)雜。

我在這篇文章中使用的PHP版本是PHP 4.3.4 for Win32,可以從The PHP Group下載。MySQL的版本是MySQL 4.0.16 for Win32,可以從MySQL.com得到。MySQL的安裝很容易——只要簡(jiǎn)單地按照其指令來(lái)就可以了。PHP稍微有一點(diǎn)復(fù)雜。

在PHP的下載頁(yè)面有兩個(gè)文件:一個(gè)ZIP文件和一個(gè)安裝文件。因?yàn)槲覀冃枰砑覼IP文件中的擴(kuò)展,所以這兩個(gè)文件都要下載。下面是下載之后的所要做的一個(gè)簡(jiǎn)單步驟:

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

2. 解壓iconv.dll,將其放到Windows的系統(tǒng)文件夾中。

3. PHP安裝目錄下創(chuàng)建一個(gè)目錄(默認(rèn)為C:\PHP)“extensions”。

4. 解壓php_domxml.dll文件到這個(gè)目錄。

5.? 在Windows文件夾下找到php.ini文件,然后使用記事本或其它文本編輯器打開(kāi)。在這個(gè)文件中找到“extensions_dir=”,然后將其值修改為第3步設(shè)置的擴(kuò)展文件夾的完整路徑。

6. 找到“;extension=php_domxml.dll”,刪除本行開(kāi)頭的分號(hào)。

7.重新啟動(dòng)Web服務(wù)器。

然后在你的Web目錄下使用下面的代碼創(chuàng)建一個(gè)PHP頁(yè)面“test.php”。(這段代碼在運(yùn)行IIS 5.0的Windows 2000 SP3能夠正常運(yùn)行。)

$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...
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Why We Comment: A PHP Guide Why We Comment: A PHP Guide Jul 15, 2025 am 02:48 AM

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

PHP 8 Installation Guide PHP 8 Installation Guide Jul 16, 2025 am 03:41 AM

The steps to install PHP8 on Ubuntu are: 1. Update the software package list; 2. Install PHP8 and basic components; 3. Check the version to confirm that the installation is successful; 4. Install additional modules as needed. Windows users can download and decompress the ZIP package, then modify the configuration file, enable extensions, and add the path to environment variables. macOS users recommend using Homebrew to install, and perform steps such as adding tap, installing PHP8, setting the default version and verifying the version. Although the installation methods are different under different systems, the process is clear, so you can choose the right method according to the purpose.

What is PHP and What is it Used For? What is PHP and What is it Used For? Jul 16, 2025 am 03:45 AM

PHPisaserver-sidescriptinglanguageusedforwebdevelopment,especiallyfordynamicwebsitesandCMSplatformslikeWordPress.Itrunsontheserver,processesdata,interactswithdatabases,andsendsHTMLtobrowsers.Commonusesincludeuserauthentication,e-commerceplatforms,for

Can a Python class have multiple constructors? Can a Python class have multiple constructors? Jul 15, 2025 am 02:54 AM

Yes,aPythonclasscanhavemultipleconstructorsthroughalternativetechniques.1.Usedefaultargumentsinthe__init__methodtoallowflexibleinitializationwithvaryingnumbersofparameters.2.Defineclassmethodsasalternativeconstructorsforclearerandscalableobjectcreati

python if else example python if else example Jul 15, 2025 am 02:55 AM

The key to writing Python's ifelse statements is to understand the logical structure and details. 1. The infrastructure is to execute a piece of code if conditions are established, otherwise the else part is executed, else is optional; 2. Multi-condition judgment is implemented with elif, and it is executed sequentially and stopped once it is met; 3. Nested if is used for further subdivision judgment, it is recommended not to exceed two layers; 4. A ternary expression can be used to replace simple ifelse in a simple scenario. Only by paying attention to indentation, conditional order and logical integrity can we write clear and stable judgment codes.

Your First PHP Script: A Practical Introduction Your First PHP Script: A Practical Introduction Jul 16, 2025 am 03:42 AM

How to start writing your first PHP script? First, set up the local development environment, install XAMPP/MAMP/LAMP, and use a text editor to understand the server's running principle. Secondly, create a file called hello.php, enter the basic code and run the test. Third, learn to use PHP and HTML to achieve dynamic content output. Finally, pay attention to common errors such as missing semicolons, citation issues, and file extension errors, and enable error reports for debugging.

How Do You Handle File Operations (Reading/Writing) in PHP? How Do You Handle File Operations (Reading/Writing) in PHP? Jul 16, 2025 am 03:48 AM

TohandlefileoperationsinPHP,useappropriatefunctionsandmodes.1.Toreadafile,usefile_get_contents()forsmallfilesorfgets()inaloopforline-by-lineprocessing.2.Towritetoafile,usefile_put_contents()forsimplewritesorappendingwiththeFILE_APPENDflag,orfwrite()w

PHP remove whitespace from string PHP remove whitespace from string Jul 15, 2025 am 02:51 AM

There are three main ways to remove spaces in PHP strings. First, use the trim() function to remove whitespace characters at both ends of the string, such as spaces, tabs, line breaks, etc.; if only the beginning or end spaces need to be removed, use ltrim() or rtrim() respectively. Secondly, using str_replace('','',$str) can delete all space characters in the string, but will not affect other types of whitespace, such as tabs or newlines. Finally, if you need to completely clear all whitespace characters including spaces, tabs, and line breaks, it is recommended to use preg_replace('/\s /','',$str) to achieve more flexible cleaning through regular expressions. Choose the right one according to the specific needs

See all articles