Home Backend Development PHP Tutorial 用MySQL和PHP创建XML

用MySQL和PHP创建XML

Jun 01, 2016 pm 02:28 PM

 xml是作为数据交换标准而出现的。java和.NET都内在支持XML,但是其它平台上的开发者也不用担心自己被这项技术所遗弃了。php就是一个颇受欢迎的支持XML的脚本环境。
 

在本文中,我将向你介绍利用诸如PHP和MySQL这样的开放源码工具来处理XML的过程。我将以执行一个MySQL查询并把从查询所得到数据格式化为XML为例来加以说明。最后,在开始埋头编写代码之前,我将介绍如何把XML写入文件并检测系统设置。

为了可以运行本文所给出的代码,你需要能过运行PHP和MySQL,而且为了充分利用这些例子,你还需要知道主机名、用户名和口令。例子中的MySQL数据库的格式如图A所示。让我们看看如何用PHP来连接该数据库。

图A

用MySQL和PHP创建XML



用PHP建立数据库连接

下面的PHP脚本建立到数据库的连接并执行一个查询:
 
 $db_name = "xrandomusa_4";
$connection = mysql_connect("MySQL.somewhere.com", "username", "passWord") or die("Could not connect.");
$table_name = 'pages';

查询MySQL

在MySQL连接建立之后,你必须用该连接来建立当前数据库。下面的代码就是用来完成这个任务的:
 
$db = mysql_select_db($dbName, $link);

现在,用一个SQL语句来选择$table_name中的所有行:
$query = "select * from " . $table_name;

如有必要,你可以随后添加属性。现在,如下执行查询:
$result = mysql_query($query, $connection) or die("Could not complete database query");
$num = mysql_num_rows($result);


你可以通过PHP.net网站来获得关于所有MySQL函数的参考资料。

建立并编写XML

现在,你应该已经做好了建立新XML文档的准备了。这有许多种方法,不过我认为清单A中所用到的方法可以满足绝大多数目的。

现在我们详细介绍这一过程。变量num表示你查询的数据行出现与否,它对MySQL的mysql_num_rows函数是可测的。变量$file包含了一个指针,该指针指向PHP在文件系统中成功的搜索到results.xml时所产生文件对象。如果找到result.xml,则创建你的PHP文件对象以及指定文件,且其属性是可写的。现在你可以把一个变量的内容打印到创建后的文件(由于你的目录已经设置为允许PHP写文件,所以这么做是没有问题的)。

注意,从安全的角度来考虑,在实际的应用程序开发中这么做其实是很愚蠢的。为了确保你能够安全的实现本文所涉及的概念,你应该为你所希望打开并写入的文件提供完全路径,并确保该文件位于你的Web根目录之下。

然后,PHP的mysql_fetch_array函数把查询变量$result转换成一个数组,并按其关键字进行循环。如果pgaeTitle在查询所返回的栏之中,对每一行返回值都向字符串变量$_xml写入某些文本。

注意运算符“.=”(它用来把XML格式的字符串当作数值来拼接)读取$row。当循环结束的时候,根XML节点打印到变量$_xml之中,所有的变量用PHP的fwrite函数写入file.xml文件之中。

现在,屏幕上显示出一个链接。确保这条链接指向你的XML文件的路径,否则你无法看到这个格式化XML(该文件用PHP处理MySQL查询而产生)。

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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles