Phonegap build is not compatible with php. How to access a database?

Asked

Viewed 758 times

0

I created an html website and accessed the mysql database by php. I passed the application to my android through the adobe cloud phonegap build and I realized that it will not fetch the data by php because this is on the server side. What can I do to get access to a database? Thank you. As you can see my app has gone like this:

inserir a descrição da imagem aqui

1 answer

3

As well as the home page of Phonegap says:

Easily create apps using the web technologies we know and love: HTML, CSS and Javascript

In other words, it only accepts these 3 languages and PHP is out of this context. Already answering your question of how you have access to a database within your App there are 3 native forms, 1 external form and two other "temporary", with further explanation.

1) Putting a database inside your App, currently HTML5 together with Javascript have implemented database access natively and offline, ie the browser itself can work with some kind of databases I explain below.

  • Sqlite: The most famous and simple at the same time, basically it is a small library, which when started in your project, creates a small file inside your application and this file will be your database, and you can give INSERT, UPDATE, CREATE and all basic commands existing in the language SQL, with the advantage of being light and agile, as well as the possibility to backup only copying this generated file and be possible to use it in other languages with support for Sqlite like PHP, C#, Java and others. There are several tutorials and codes on the web at the beginning - Offline Sqlite on Mobile with HTML5 - but recommend reading as much as possible to learn about it.

  • Websql: Websql has the concept WELL similar to Sqlite (some say it’s just the built-in Sqlite within the browser) the difference is that it was created by W3C is that this library is focused only on the use within the browser, it has its particularities also in terms of functioning but not many, another detail is that not all browsers still this with this library in full functionality, I can recommend this tutorial for reading - Working with the HTML5 Web SQL Database - but unlike Sqlite I believe you will find a smaller amount of documentation and examples while this technology does not become more mature.

  • Indexdb: Well it is also a database that works internally only within the browser, but its biggest difference between Websql and Sqlite is that it does not work with SQL like the previous ones but with keys and values JSON so it’s more like a database "Nonrelational (Nosql)" than a database "Relational (SQL)".

2) Well the second way to do that is to use external means, like a API, hosting on an external machine and making your application access there remotely, the disadvantage is that the user is forced to be always with internet, access time to API, security and the increased complexity of the system, but in some cases it is necessary to use an API, such as a chat system where the message leaves one place and goes to another needs a control point, or a Facebook-style comment system.

In your case, as already said to have a piece of code ready in PHP, you would have to host these your files on a server, and make some adaptations so that the calls return JSON or XML for example and these in turn were presented in your App. For that you could even use the Jquery Mobile to create the calls and views of the contents, I will give a small example below:

$.ajax({
 url: "http://seudominio.com/catalogo.php",
 success: function(result) {
  alert(result); // Mostra os dados gerado pelo PHP + DB
 } 
});

3) A third method of recording information in an App would be through Cookies or localStorage, since we are talking about a hybrid App (through Phonegap) nothing more fair than using Web technologies, and I say that if you need to record small information and do not need security would say that this is the guy indicated, I even used cookies in a little game I made with Phonegap, in my case I recorded the last score reached by the user and served me well.

  • Cookies: Well this technology you must have heard the exhaustion out there, it consists of a small file that gets a name and a value, you can record, read and delete this small file at any time you want, and Javascript can do all these operations, here is a tutorial on how to do this - Javascript Cookies.

  • localStorage: This technology is new and has been implemented in HTML5, but it is very similar to cookies the advantage of it is the storage size (5MB), better read and write time without affecting the performance and improvement of data security, here you will find a tutorial too - HTML5 Local Storage.

PS. I put several links, I know it’s not recommended, but the answer to that question is very broad, and it involves many codes and examples, not to mention that you need to see what technology she intends to use, what her real need is, and how much you know about these technologies, so it’s just a direction and not a tutorial on how to do.

  • API, for me, is the best option. A hybrid might be ideal, saving some data on the client side and others collecting (and updating) via API.

  • Sad to see such a complete and useful answer not marked as a solution nor +1 by the questioner...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.