Flex中的MySQL管理(1)
学习使用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> Copy after login |
该脚本的第一项工作就是利用MDB2库连接数据库。如果没有安装MDB2库,则可使用PEAR安装该库,如下所示:
% pear install MDB2<br>% Copy after login |
如果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> Copy after login |
在本例中,我并未指定数据库或模式,这会要求脚本返回可用数据库的清单。假如web服务脚本有权执行该任务,则在curl语句后面就会显示执行的结果。在本例中,将以标签的形式显示不同数据库的列表。
该脚本返回的所有数据都带有
除了使用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>% Copy after login |
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> Copy after login |
表格中有两条记录:第一条显示Apple公司将发布超炫的IPhone;第二条显示Google公司将发布同样很炫、但用途完全不同的Gears系统。
在本地机器上安装了极为强大且灵活的后台程序后,就可以着手为其创建Flex前台程序了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.
