Flash可以与SQL集成吗? [英] Can Flash be integrated with SQL?

查看:161
本文介绍了Flash可以与SQL集成吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Flash可以和SQL一起使用吗?我有一个Flash窗体,我需要连接到SQL。如果网络上有任何关于这个话题的例子。我找不到它。

解决方案

您不直接使用ActionScript与SQL数据库。相反,您可以从ActionScript向服务器发出http请求,并指定正确的参数。一个典型的开源设置是与MySQL DB进行通信的PHP脚本,但是您可以使用Java与Oracle,带CouchDB的Ruby,带SQL的.NET或任何其他可能的配置。重要的一点是,您必须能够调用服务器脚本并传递变量...通常是Restful设置。

一旦您的PHP脚本被正确配置,您可以使用http POST或http GET从ActionScript发送值。

PHP:

 <?php 
$ updateValue = $ _POST [updateValue];
$ dbResult = updateDB($ updateValue); //这应该返回db响应
echo($ dbResult);
?>

要从ActionScript调用此脚本,您需要创建一个变量对象。
$ b

  var变量:URLVariables = new URLVariables(); 
variables.updateValue =someResult;

变量名称.updateValue必须完全匹配php变量。



现在创建一个URLRequest对象,指定脚本的位置。对于这个例子,该方法必须设置为POST。

  var request:URLRequest = new URLRequest(yourScript.php ); 
request.method = URLRequestMethod.POST;
request.data =变量;

现在创建一个URLLoader并添加一个事件监听器。

  var loader:URLLoader = new URLLoader() ; 
loader.addEventListener(Event.COMPLETE,onComplete);
loader.load(request);

处理程序看起来像这样。

  private function onComplete(e:Event):void 
{
trace(URLLoader(e.target ).data.toString());

$



这个例子展示了如何更新和接收来自server / db组合的响应。但是,您也可以通过脚本查询数据库并解析结果。因此,在上面的PHP示例中,可以输出JSON,XML或者管道字符串,这可以被ActionScript使用。



XML是流行的选择,因为ActionScript e4x支持将XML视为本地对象。



为了像上面的XML响应一样处理上面的响应,请在onComplete处理程序中使用以下内容。

  private function onComplete(e:Event):void 
{
var result:XML = XML(URLLoader(e.target).data );



$ b $ p
$ b

如果你的xml格式不正确,会引发错误,所以请确保服务器脚本始终打印出有效的XML,即使存在数据库错误。


Can Flash be used together with SQL? I have a Flash form and I need to connect it to SQL. If there is any example on the net about this topic. I can't find it.

解决方案

You don't use ActionScript directly with an SQL database. Instead you make http requests from ActionScript to a server, specifying the correct parameters. A typical opensource setup, is a PHP script communicating with a MySQL DB, but you can use Java with Oracle, Ruby with CouchDB, .NET with SQL or any other possible configuration. The important point is that you must be able to call a server script and pass variables... typically a Restful setup.

Once your PHP script has been properly configured, you can use http POST or http GET to send values from ActionScript.

PHP:

<?php
    $updateValue = $_POST["updateValue"];
    $dbResult = updateDB( $updateValue ); //This should return the db response
    echo( $dbResult );
?>

To call this script from ActionScript, you need to create a variables object.

var variables:URLVariables = new URLVariables();
variables.updateValue = "someResult";

The variable name .updateValue, must match the php variable exactly.

now create a URLRequest Object, specifying the location of your script. For this example the method must be set to POST. You add the variable above to the data setter of the request.

var request:URLRequest = new URLRequest( "yourScript.php" );
request.method = URLRequestMethod.POST;
request.data = variables;

Now create a URLLoader and add an event listener. Do not pass the request created above to the constructor, but to the load method.

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete );
loader.load( request );

The handler would look something like this.

private function onComplete( e:Event ) : void
{
    trace( URLLoader( e.target ).data.toString() );
}

This example shows how to update and receive a response from a server / db combo. However, you can also query a DB through the script and parse the result. So in the PHP example above, you can output JSON, XML or even a piped string, and this can be consumed by ActionScript.

XML is a popular choice, as ActionScript's e4x support treats XML like a native object.

To treat the response above like an XML response, use the following in the onComplete handler.

private function onComplete( e:Event ) : void
{
    var result:XML = XML( URLLoader( e.target ).data );
}

This will throw an error if your xml is poorly formed, so ensure the server script always prints out valid XML, even if there is a DB error.

这篇关于Flash可以与SQL集成吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆