Search code examples
sencha-touchsencha-touch-2

how to connect MySql db with sencha touch application


I'm new to Sencha and have been reading tons of documentation but what I haven't been able to find is the proper way to make a connection to my MySQL DB, Could someone point me in the right direction.


Solution

  • You don't really "connect" to a database with a Sencha Touch application. You have to create some kind of API for it to interact with. So wherever you database is hosted you might have a file like "users.php", inside that file you would have code that pulls whatever you need from the database (or inserts data into the database) and then outputs it to the browser in a JSON format.

    Then in your Sencha Touch application you would make an Ajax request to that page like this:

        Ext.Ajax.request({
    
            url: 'https://www.example.com/users.php',
            method: 'post',
            params: {
                data: data
            },
            success: function(response){
    
            }
        });
    

    or you could define a proxy in your models or stores like this:

        proxy: {
            type: 'ajax',
            api: {
                create: 'http://example.com/users.php?action=create',
                read: 'http://example.com/users.php?action=read',
                update: 'http://example.com/users.php?action=update',
                destroy: 'http://example.com/users.php?action=destroy'
            },
            reader: {
                rootProperty: 'users'
            }
        }
    

    I've written a pretty in depth tutorial if you want some more details: http://www.joshmorony.com/part-1-sencha-touch-email-facebook-log-in-system-with-php-mysql-backend/