php - 下载sql表的值以供离线重用

标签 php mysql actionscript-3 air local-storage

我有一个 Flash 应用程序,它调用在线 php 文件来读取 SQL 表的一些值。

所以我的 AS3 代码中有这样一行:

var urlReq:URLRequest = new URLRequest ("http://www.****.com/sql_result.php");

这在我的 php 中:

 $connection = mysql_connect("mysql***.perso", "test", "password") or die ("Couldn't connect to the server.");

问题:如果用户离线,则无法访问这些值。

有没有办法下载带有 AS3 代码的 SQL 表(当用户有互联网时)以便离线访问它。

喜欢:

function onConnection(e:Event = null):void{
if(monitor.available)
            {
                trace("You are connected to the internet");
                read_php_online();
            }
            else
            {
                trace("You are not connected to the internet");
                read_php_offline();
            }

            monitor.stop();
        }

function read_php_offline():void{
    var urlReq:URLRequest = new URLRequest ("local/sql_result_offline.php");
..
..
}

为了访问离线 SQL 表,应该有 sql_result_offline.php 吗?

 $connection = mysql_connect("LOCAL", "user", "password");

谢谢,

最佳答案

对于 FLASH:

要使用 Flash 在本地保存数据,您可以使用 3 种方式之一:Flash Player 缓存、SharedObject ,或 FileReference目的。对于您的本地文件,请忘记 PHP 和 MySQL,因为我们只讨论您获得的数据(json、xml、txt...)。

- Flash Player 缓存:

您应该知道,默认情况下,Flash Player 会将文件的本地副本放入其缓存中。您可以使用此本地副本作为数据的离线源,但请不要忘记 Flash Player 并未保存远程文件的最后一个版本,而是保存第一个版本和 http://www.example.com/data.phphttp://www.example.com/data.php?123 不同即使是同一个文件!有关详细信息,请查看my answer of this question

- 共享对象:

我不知道您加载的数据的大小,但正如 Adob​​e 关于 SharedObject 所说:

... is used to read and store limited amounts of data on a user's computer ...

我认为这不适用于大文件,也不建议存储文件,而是存储一些简单的数据。当然,SharedOject作为浏览器的cookie,需要用户授权才能将数据写入硬盘,并且用户可以随时删除。

- 文件引用:

我认为这是实现您正在寻找的目标的最佳方式。您应该知道,要使用 FileReference 保存文件,您的用户会被邀请选择一个文件来保存数据并再次读取它。因此,如果您不希望任何用户与您的应用程序交互,请忘记这种方式。

文件引用使用示例:

var local_file_name:String = 'local.data',
    file:FileReference = new FileReference(),
    local_file_filter:FileFilter = new FileFilter('local data file', '*.data'),
    remote_data_url:String = 'http://www.example.com/data.php',
    url_request:URLRequest,
    url_loader:URLLoader,       
    connected:Boolean = true;

if(connected){
    get_remote_data();
} else {
    get_local_data();
}

function get_remote_data(): void {
    //we use a param to be sure that we have always the last version of our file
    url_request = new URLRequest(remote_data_url + ('?' + new Date().getTime()));
    url_loader = new URLLoader();
    url_loader.addEventListener(Event.COMPLETE, on_data_loaded);
    url_loader.load(url_request);           
}

function get_local_data(): void {
    // show the select dialog to the user to select the local data file
    file.browse([local_file_filter]);
    file.addEventListener(Event.SELECT, on_file_selected);          
}

function on_data_loaded(e:Event): void {
    var data:String = e.target.data;
    // if the remote data is successfully loaded, save it on a local file 
    if(connected){
        // show the save dialog and save data to a local file
        file.save(data, local_file_name);
    }
    // use your loaded data
    trace(data);            
}

function on_file_selected(e:Event): void {
    file.addEventListener(Event.COMPLETE, on_data_loaded);
    file.load();
}

此代码将在每次向用户显示保存对话框时显示,当然,这只是一个示例,您必须根据您的需要调整它......

编辑

对于空气:

对于 AIR,我们不需要 FileReference 对象,而是使用 File和一个FileStream保存数据的对象:

// for example, our local file will be saved in the same dir of our AIR app
var file:File = new File( File.applicationDirectory.resolvePath('local.data').nativePath ),
    remote_data_url:String = 'http://www.example.com/data.php',
    data_url:String = remote_data_url,
    url_request:URLRequest,
    url_loader:URLLoader,       
    connected:Boolean = true;

if(!connected){
    // if we are not connected, we use the path of the local file
    data_url = file.nativePath;     
}

load_data();

function load_data(): void {
    url_request = new URLRequest(data_url);
    url_loader = new URLLoader();
    url_loader.addEventListener(Event.COMPLETE, on_data_loaded);
    url_loader.load(url_request);           
}

function on_data_loaded(e:Event): void {
    var data:String = e.target.data;
    if(connected){          
        // save data to the local file
        var file_stream:FileStream = new FileStream();
            file_stream.open(file, FileMode.WRITE);
            file_stream.writeUTFBytes(data);
            file_stream.close();
    }
    trace(data);            
}

希望能有所帮助。

关于php - 下载sql表的值以供离线重用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31041066/

相关文章:

php - .htaccess 文件、PHP、包含目录和 Windows XAMPP 配置噩梦

php - 如何在 Azure 应用程序服务 -> Web 应用程序 (Linux) 中启用 php 扩展

apache-flex - 弹性 3 和空气 2 : Automatically detect when files on a directory are updated

actionscript-3 - 文件引用与文件?

PHP MYSQL 表单不会更新数据库

php - 使用 PHP 和 MySQL 单击复选框时启用保存到数据库

php - 连接到数据库而不写入警告/错误消息

mysql - 如何创建到 dockerfile/mysql 容器的 ssh 隧道

Mysql RDS 认证安全

actionscript-3 - 使用 URLRequest POST 文件上传