download values of sql table for offline reuse

I’ve got a Flash app that calls an online php file in order to read some values of my SQL table.

So I’ve got a line like this in my AS3 code:

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

And this in my php :

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

Problem : if the user is offline he can’t access the values.

Is there way to download the SQL table with AS3 code (when the user have internet) in order to access it offline.

Like :

function onConnection(e:Event = null):void{
                trace("You are connected to the internet");
                trace("You are not connected to the internet");


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

And what should have sql_result_offline.php in order to access an offline SQL Table ?

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

Thank you,


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1


To save data locally with flash, you can use one of 3 manners : the Flash Player cache, a SharedObject, or a FileReference object. And for your local file, forget PHP and MySQL because we are speaking only about the data that you got ( json, xml, txt, … ).

– Flash Player cache :

You should know that by default, flash player put a local copy of your file in its cache. You can use this local copy as an offline source of your data, but here don’t forget that flash player didn’t save the last version of your remote file but the first one and that is different from even if it’s the same file ! For more details about that, take a look on my answer of this question.

– SharedObject :

I don’t know the size of your loaded data, but as Adobe said about SharedObject :

… is used to read and store limited amounts of data on a user’s computer …

I think that is not used for large files and it’s not recommended to store files but some simple data. Of course, as a cookie for the browser, SharedOject needs user’s authorization to write data to the hard drive, and user can delete it at any time.

– FileReference :

I think this is the best manner to do what you are looking for. You should know that to save a file using FileReference, your user is invited to select a file for saving data and reading it in a second time. So if you don’t want any user’s interaction with your application, forget this manner.

FileReference using example :

var local_file_name:String = '',
    file:FileReference = new FileReference(),
    local_file_filter:FileFilter = new FileFilter('local data file', '*.data'),
    remote_data_url:String = '',
    connected:Boolean = true;

} else {

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);

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

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

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

This code will show every time a save dialog to the user, of course, it’s just a sample, you have to adapt it to your needs …


For AIR :

With AIR we don’t need a FileReference object, instead we use File and a FileStream object to save data :

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

    // if we are not connected, we use the path of the local file
    data_url = file.nativePath;     


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

function on_data_loaded(e:Event): void {
    var data:String =;
        // save data to the local file
        var file_stream:FileStream = new FileStream();
  , FileMode.WRITE);

Hope that can help.

Method 2

you have a flash swf, mobile app or air app?

Storing local data

you can use file as database (like csv), for mobile and air you can use local SQLite database.

if you have native desktop app – it is possible to use mysql, via native process or native extension but it is not so easy..

  • edit:
    Working with local SQL databases in AIR
    [+] you can keep your data safe- with encryption, a password at startup and etc.
    [-] it will require a lot more of code
    (create database after install, sync regularly, get data from local database if no internet conn.) mysql and sqlite have some differences also (like “insert or update” statement for sqlite)

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x