Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Robert_C_6
New Contributor I
215 Views

How to connect my app to a mysql database

Jump to solution

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

Is that something that is possible with Intel XDK?

Tags (2)
0 Kudos
1 Solution
Diego_Calp
Valued Contributor I
215 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

10 Replies
Elroy_A_Intel
Employee
215 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.

Diego_Calp
Valued Contributor I
216 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

Nick_F_2
New Contributor III
215 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.

 

Fab_V_
New Contributor I
215 Views

I personally use webservices to retrieve JSON datas from website.

Robert_C_6
New Contributor I
215 Views

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

Kiriakos_T_
Beginner
215 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?

Alex_Hang
New Contributor II
215 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);

 

Diego_Calp
Valued Contributor I
215 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

Kiriakos_T_
Beginner
215 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?

Diego_Calp
Valued Contributor I
215 Views

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

Regards

Diego

 

Reply