search
HomeDatabaseMysql TutorialFlex中的MySQL管理(1)

Flex中的MySQL管理(1)

Jun 07, 2016 pm 04:06 PM
flexFRAmysqlusestudymanage

学习使用RIA Framework Flex创建MySQL管理UI PHPMyAdmin的出现震撼了业界,这毫无疑问。它当然是基于PHP的最佳应用程序,因为它将MySQL管理界面由命令行的形式改为了web浏览器的形式。不过,虽然它的功能很强大,但使用并不太方便,界面也不够美观。因此,我

学习使用RIA Framework Flex创建MySQL管理UI

PHPMyAdmin的出现震撼了业界,这毫无疑问。它当然是基于PHP的最佳应用程序,因为它将MySQL管理界面由命令行的形式改为了web浏览器的形式。不过,虽然它的功能很强大,但使用并不太方便,界面也不够美观。因此,我尝试通过Rich Internet Application框架设计更理想的MySQL前台管理程序。

要达成此目标本可选用Ajax。但我不想处理客户端的不兼容问题。当然,Silverlight也是不错的选择,但它仍不够成熟。 之所以选择Adobe Flex,是因为它拥有富用户接口工具集和方便的web服务集成功能,而且它生成的Flash应用程序能够以相同方式在任何操作系统中运行。

我学习了很多有关创建应用程序方面的知识:如何为PHP程序创建安全的SQL web服务;如何通过Flex访问web服务;如何将web服务返回的数据输入数据网格中并显示。在本文中,我将引领读者从前台到后台,逐步创建MySQL管理程序。读者从中可了解一些有用的信息,以创建自己的Rich Internet应用程序。

创建后台程序

Flex应用程序擅长与web服务通讯,以发出请求及提交数据。因此,首先需要创建一个非常简单的PHP脚本,它将以XML格式返回数据库列表、表或表中的数据。

清单1:req.php

<p><?php <br>require_once("MDB2.php");<br>$sql = 'SHOW DATABASES';<br>if ( $_REQUEST['mode'] == 'getTables' )<br>$sql = 'SHOW TABLES';<br>if ( $_REQUEST['mode'] == 'getData' )<br>$sql = 'SELECT * FROM '.$_REQUEST['table'];<br>$dsn = 'mysql://root@localhost/'.$_REQUEST['db'];<br>$mdb2 =& MDB2::factory($dsn);<br>if (PEAR::isError($mdb2)) { die($mdb2->getMessage()); }<br>$dom = new DomDocument();<br>$dom->formatOutput = true;<br>$root = $dom->createElement( "records" );<br>$dom->appendChild( $root );<br>$res =& $mdb2->query( $sql );<br>if (PEAR::isError($mdb2)) { die($mdb2->getMessage()); }<br>while ($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC))<br>{<br>$rec = $dom->createElement( "record" );<br>$root->appendChild( $rec );<br>foreach( array_keys( $row ) as $key ) {<br>$key_elem = $dom->createElement( $key );<br>$rec->appendChild( $key_elem );<br>$key_elem->appendChild( $dom->createTextNode( $row[$key] ) );<br>}<br>}<br>$res->free();<br>$mdb2->disconnect();<br>header( "Content-type: text/xml" );<br>echo $dom->saveXML();<br>?></p>

该脚本的第一项工作就是利用MDB2库连接数据库。如果没有安装MDB2库,则可使用PEAR安装该库,如下所示:

% pear install MDB2<br>% 

如果PEAR无法正常运行,可访问http://pear.php.net/mdb2,然后下载源代码并将其解包到PHP的include路径下。MDB2是通用的数据库适配器层,它已取代了广为使用的PEAR DB库。

脚本的第二项工作就是创建XML DOM Document对象,该对象将用来创建要输出的XML树。从此处开始,它将运行查询,并在XML树中添加row和column作为XML标签。最后,该脚本将关闭所有连接,并将XML保存到PHP输出流中。

选用XML DOM对象的原因是,它可避免任何与数据、不对称标签等有关的编码问题以及各种可能使XML产生混乱的因素。我可以将调试XML数据流的时间节省下来做其他许多更有意义的工作。您一定也会这样做。

将该脚本安装到本地机器上的可运行目录下,然后使用curl命令向服务器发出请求。

% curl "http://localhost/sql/req.php"<br><?xml version="1.0"?><br><records><br><record><br><database>addresses</database><br></record><br><record><br><database>ajaxdb</database><br></record><br>...<br>% </records>

在本例中,我并未指定数据库或模式,这会要求脚本返回可用数据库的清单。假如web服务脚本有权执行该任务,则在curl语句后面就会显示执行的结果。在本例中,将以标签的形式显示不同数据库的列表。

该脚本返回的所有数据都带有标签,它包含一组标签。每个标签的名称和内容取决于已执行的SQL语句。在本例中,只返回单列命名数据库。

除了使用curl命令,还可将URL输入浏览器中,然后在加载页面后选择“View Source(查看源文件)”。

在下例中,将连接articles数据库并获取它的表格列表。结果如下:

% curl ".../req.php?mode=getTables&db=articles"<br><?xml version="1.0"?><br><records><br><record><br><tables_in_articles>article</tables_in_articles><br></record><br></records><br>% 

articles数据库中只有一个名为article的表格,这并不奇怪。要运行经典的select * from article查询以获取所有记录,可使用以下URL:

<p>% curl ".../req.php?mode=getData&db=articles&table=article"<br><?xml version="1.0"?><br><records><br><record><br><id>1</id><br><title>Apple releases iPhone</title>
<br><content>Apple Computer is going to release the iPhone...</content><br></record><br><record><br><id>2</id><br><title>Google release Gears</title> <br><content>Google, Inc. of Mountain View California has...</content><br></record><br></records><br>%</p>

表格中有两条记录:第一条显示Apple公司将发布超炫的IPhone;第二条显示Google公司将发布同样很炫、但用途完全不同的Gears系统。

在本地机器上安装了极为强大且灵活的后台程序后,就可以着手为其创建Flex前台程序了。

Statement
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
What are stored procedures in MySQL?What are stored procedures in MySQL?May 01, 2025 am 12:27 AM

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

How does query caching work in MySQL?How does query caching work in MySQL?May 01, 2025 am 12:26 AM

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

What are the advantages of using MySQL over other relational databases?What are the advantages of using MySQL over other relational databases?May 01, 2025 am 12:18 AM

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

How do you handle database upgrades in MySQL?How do you handle database upgrades in MySQL?Apr 30, 2025 am 12:28 AM

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

What are the different backup strategies you can use for MySQL?What are the different backup strategies you can use for MySQL?Apr 30, 2025 am 12:28 AM

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

What is MySQL clustering?What is MySQL clustering?Apr 30, 2025 am 12:28 AM

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

How do you optimize database schema design for performance in MySQL?How do you optimize database schema design for performance in MySQL?Apr 30, 2025 am 12:27 AM

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

How can you optimize MySQL performance?How can you optimize MySQL performance?Apr 30, 2025 am 12:26 AM

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi

See all articles

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 Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)