Software Archive
Read-only legacy content

How to connect my app to a mysql database

Robert_C_6
New Contributor I
1,148 Views

I want to connect my app to a mysql database to query and display information.

Is that something that is possible with Intel XDK?

0 Kudos
1 Solution
Diego_Calp
Valued Contributor I
1,148 Views

Hi Robert,

I'm using Dreamfactory to access MySQL from XDK (www.dreamfactory.com). It is an opensource tool that generates automatically the REST API that Elroy is suggesting to implement just connecting your database. It works very well, handles autentication and has a lot of features more.

Additional to MySQL, it works with several databases more, including some NoSQL, I think it worth take a look and dedicate some time to learn it and testing.

Once you get the basics feel free to PM me for some sample code to use it from XDK.

Regards,

Diego

View solution in original post

0 Kudos
10 Replies
Elroy_A_Intel
Employee
1,148 Views

If you have a a mysql database on a server, I recommend implementing REST API on the server to allow access to the database entries from the client. Intel XDK allows you to create the application. It doesn't contain a feature to implement this for you.

0 Kudos
Diego_Calp
Valued Contributor I
1,149 Views

Hi Robert,

I'm using Dreamfactory to access MySQL from XDK (www.dreamfactory.com). It is an opensource tool that generates automatically the REST API that Elroy is suggesting to implement just connecting your database. It works very well, handles autentication and has a lot of features more.

Additional to MySQL, it works with several databases more, including some NoSQL, I think it worth take a look and dedicate some time to learn it and testing.

Once you get the basics feel free to PM me for some sample code to use it from XDK.

Regards,

Diego

0 Kudos
Nick_F_2
New Contributor III
1,148 Views

Hi Robert

I just create a web service on the mysql server. The web service can be php or asp.net and can return JSON data to your app.

Please search my other postings for example code.

 

0 Kudos
Fab_V_
New Contributor I
1,148 Views

I personally use webservices to retrieve JSON datas from website.

0 Kudos
Robert_C_6
New Contributor I
1,148 Views

I am going to give Dream Factory a try, If not I will try the other options

0 Kudos
Kiriakos_T_
Beginner
1,148 Views

Hi Diego,

I am developing an app for taking photos and location and upload them in a database (propably in phpMyAdmin). I also started using dreamfactory. Can you provide me some help on connecting the database with the xdk?

0 Kudos
Alex_Hang
New Contributor II
1,148 Views

You can just use pure PHP, doing everything inside an iframe, that you host on a free hosting service.

This is the code you can use to connect to SQL

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

If you want to execute a query, you must use a code like this one:

$sql="Select * from Table_name";
$conn->query($sql);

 

0 Kudos
Diego_Calp
Valued Contributor I
1,148 Views

Hi Kiriakos,

Here is some sample code, it is for DreamFactory 2 or above.

 

//this function to connect, with a user defined in DF

function logindf(mail, pass){	
	$.ajax({
		type:'POST',
		beforeSend: function (request) {
			request.setRequestHeader('Content-Type','application/json');
			request.setRequestHeader('X-DreamFactory-Api-Key','YourAPIKEY');
		},
        //urldf is your DreamFactory URL
		url: urlDf+'api/v2/user/session',

		data: '{"email":"'+mail+'", "password":"'+pass+'"}',
		success: function (response) {
            //save session in a global variable to use in other DF calls
            sesion = response.session_id;
		},
		error: function (response) {

            alert('Se ha producido un error de acceso, por favor intente nuevamente');            
			console.log(JSON.stringify(response));
		}
	});	


//this is an example to get some data from a table
    $.ajax({
		type:'GET',
		beforeSend: function (request) {
			request.setRequestHeader('X-DreamFactory-Application-Name', service); //the service name you set in DF
			request.setRequestHeader('X-DreamFactory-Session-Token', sesion);	//the session from the login function
			request.setRequestHeader('X-DreamFactory-Api-Key','YourAPIKEY');
		},
		url: urlDf+'api/v2/'+service+'/_table/yourtable',//table name
		dataType: "json",
		success: function (data) { 
			var len = data.resource.length;
			for (var i = 0; i < len; i++) {
                //process the result
			}
		},
		error: function (response) {

			alert('Se ha producido un error al consultar la base de datos, por favor intente nuevamente');
			alert(JSON.stringify(response));
		}
	});

Regards

Diego

0 Kudos
Kiriakos_T_
Beginner
1,148 Views

Dream factory should be installed in the same host of the database in order to have access to the database, am I right? if it is installed in local copmuter it will not get the appropriate database, right?

0 Kudos
Diego_Calp
Valued Contributor I
1,148 Views

Dreamfactory can connect to remote databases, you may install it anywhere.

Regards

Diego

 

0 Kudos
Reply