Connect ionic app to mysql database

Hello,
I’m new to ionic and I must say there’s something I don’t understand.
I have to connect the ionic app to a mysql database. First the user will have to create an account,and then he’ll have to login, to be able to use the app.
I think I have to create a webservice to connect my database to the app.
But I tried to find some sample on the web, and wasn’t able to find anything.
I find this rather strange. Why would I develop the code, I guess someone already did it before, so why not using something already working ?

Is there anyone who already wrote something to connect an ionic app to mysql database ?
And who would be ok to help me? (I’m using ionic 1 for now)

Thanks

3 Likes

Connecting your client app directly to the database is a bad idea. Here’s why:

In order to sign up or perform any WRITE actions, you will need WRITE permissions to the database. To achieve that, you have to leave your database credentials within the code of your client app. Doing so will lead to your data to be leaked and used in another way. Someone can use this data to erase the whole database, or worse, steal other users’ data.

The ideal way to connect your app to a database is by connecting it to a back end service. Meaning that you need to build a RESTful API that your app can talk to. Not sure how to do that? No problem. There are a lot of back end services out there, such as ionic.io. There are other alternatives, but nothing will integrate easily in your app like the Ionic Platform.

4 Likes

http://www.codeproject.com/Articles/1005150/Posting-Data-from-Ionic-App-to-PHP-Server

Yes ihadeed, you’re right, I don’t want to directly connect the app to the database. I know that I need to build a restful api, but I don’t know how. And I don’t want to use ionic.io. I’d like to write my own api in php.
I’ve been searching in internet but for now I don’t understand how to do…
I need some help.

If you’re attached to PHP, never mind, but if you’re not, may I suggest looking at Golang? gocraft/web makes a wonderful multiplexer foundation for web application servers and sqlx a great interface to SQL backends. If you don’t like those, there’s also goji for mux and gorm if you like object mappers.

Mister Maison Neuve :slight_smile: I had a careful look at your link, and it’s very interesting. However, it’s not connected to any database ?
I tested it, and I type something and it returns the string that I typed. It’s not what I’m looking for…
Actually, as ihadheed said, I need to build my own api, but as I said, I don’t know how and i feel a little lost… Everything is brand new for me :slight_smile:

Hello,
This is structure which i`m using in my apps.

//services.js
.factory('WebService', function ($http, $rootScope, $stateParams) {

		//$rootScope.url= "localhost/webapi"
        return {
            all: function () {
			
                return $http.get($rootScope.url + '/all', {
                    params: {
                        langid: $rootScope.langid
                    }
                });
            },
            save: function (id,name) {
                return $http.post($rootScope.url + '/save', {
                        params: {
                            pid: id,
                            name: name
                        }
                    })
                
            }
        };
});
//controler.js
.controller('MainCtrl', function ($scope, WebService ) {
	
	
        WebService.all().success(function (response) {
                //do something with response

            }).error(function(err){
				//do something with err
			});
			
			WebService.save('1','tutorial').success(function (response) {
                //do something with response

            }).error(function(err){
				//do something with err
			});

});

Now you just need to create web api and handle requests in proper way, and don`t forget to add

	//http://stackoverflow.com/questions/18382740/cors-not-working-php
	if (isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
    }

    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers:        
            {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

        exit(0);
    }

on your servers side if you are using php, like it mentioned in http://www.codeproject.com/Articles/1005150/Posting-Data-from-Ionic-App-to-PHP-Server

I hope that my answer will help you.

2 Likes

Thanks for your answer, but what does your api look like ?
My problem is I don’t know how to code the api…

Hey

If you wanna stick with PHP I recommend slim-franework. Very easy to use and it’s quick to build RESTful APIs with. I can provide you a little help with that if you need .

Also I highly recommend trying one of these: ExpressJS, Sails.js
They use JavaScript, which you’re already using on the front end. And it’s easy to get started with them.

1 Like

Hello,
Yes, I finally found someting with slim framework, and connected it with the app that I had.
I could send you what I did to show you if it’s ok.
Would you mind ?

Yes. Please go ahead an send it.

PM / link / email / slack … whatever you like.

I’d like to send it to you either by mail or PM but I counldn’t find neither your email address nor a link to pm you…

My email is available on my github account.
And to PM me you can do it here through the forums.

if you are using some of mvc frameworks just define routes, and beside views return some JSON data.
Otherwise test request type and parameters in php file which will communicate with your app and make some handlers for your cases from which you will return some JSON or whatever you want.
I`m personally using Laravel 5 php framework.

I think I succeeded to develop the api but now I have another problem…
For now I have a database and I successfully displayed the data, added some data, updated and deleted data.
Until this point, everything seems to be ok, after a lot of research.

But now, I need to do some login form.
I need the user to login, and then to display his data (and not all the data from the database).
My problem: how can I store the iser id somewhere ?
In a php standard web application, I would use session variables, but in this case, I can’t.

Any idea of how I can do ?

Thank you, but I can’t see your github account either. I think I may not have sufficient rights…

This will help you http://docs.ionic.io/v1.0/docs/user-quick-start

Thanks, but this guide is for users if you use ionic.io account.
In my case, I don’t want to do that.
I want to handle the users in my own database, on my own server, and not depend on another service, see what I mean ?
But thanks for your help, I appreciate.

You can use local storage or sqlite, depending on how persistent you want the credentials to be.