Android fetch data from database - php

I've decided to try out some DB connections for my android applikation. However I need some advice regarding structure.
I came to know that in order to fetch data from DB with Android I need to use php scripts. I'm a total novice in this area and for me it sounds a bit akward. Therefore I figured I could create a Java server which the application connects to, and among other things this server also fetches data from the DB and returns.
In a performance perspective, what's best? Let the application itself fetch data from the DB, or connect to the server which fetches it for you? How about security? For clearance, I will have a Java server anyhow to take care of other things.
Sorry about the Vista paint skills.
Thanks for any input!

Like others have said it doesn't matter which you use PHP vs Java. But you're on the right track. Most developers create a web service on their HTTP Server and the app talks to that. That's usually in the form of JSON or XML strings over HTTP. Popular choice is using the REST architecture which essentially tells you that stuff you access on the service are resources and you structure your service based on that.
For the PHP vs Java question it's really up to you so do which ever you can setup faster and are more familiar with. I will also say Java has productivity advantages in Android's case because you can create plain old java objects as your models for results and share that code between the server and your Android client. You get this because you can use something like Gson library which serializes and deserializes objects into JSON format. You can also use Google AppEngine for hosting your Java code too.

Well this problem is irrelevant in context to android programming i would say.
Assuming that You are returning the extracted data to your device in json format, the entire performance issue is sort of restricted to the performance of java or php in retrieving data from database and converting it to json and sending to the client.
And as far as simple operations are considered the efficiency wont matter much in both cases, it is just a matter of preference on the developers part.

Related

Create plot from database server side and visualize it on client side

I'm currently trying to find the best way of doing this:
We have a python program (client side) we use to upload metrics on a mySQL database on a server and later, via web check it and filter it, etc.
The problem arises when we try to plot any query from to the database. It's unclear to us what aproach to use. The main page was made with Joomla in php.
Currently I was looking into python alternatives to run on the server side, somehow capture the query, process the data, create the image and then return it to the client side as an image or as a string to be reconstructed on the client side. But as I have read it seems also possible (and maybe easier) to do the same in PHP or JavaScript which (as I understand it) run on the client side, leaving less to worry about.
Is it that so? Are my assumptions right? Which aproach would you use/pŕefear? Is there some link or info you could give me to continue my search?
I would prefer to do it on Python using something like matplotlib, plotly, bokeh, etc. but as I see it, the problem is not about creating the image, but about comunicating and sending the image information between server and client.
Thanks!
I think it possible to do what you want to with bokeh. I dont know joomla but it think its not that important.
Check out that part of the bokeh documentation Embedding Bockeh
then for the server parts in python i would recommend flask to start with.
You can just prototype your app with one of the bokeh server apps examples from the bokeh repo.. there are flask examples too.
Then you can start to extend the bokeh server app from the examples with a query to your database with sqlalchemy or
mysql-flask

Getting data from MYSql into my iOS app with JSON

Ok I found a few questions on how to get data from a MYSql database into an iOS app, but I am just asking a few best practices here. I know these can all be separate questions, but I am hoping that they can be answered in a way that they relate to each other.
Am I correct to understand that to be able to get data into an iOS app - I need to first generate a JSON file, have that stored on a server and than have the app download this file??
If the previous answer is NO then does that mean, I can pull in data on the fly?
Lastly I have seen PHP examples to create JSON files, but iOS is in Objective-c. Does this mean I need to load a UIWebView to be able to load the PHP page that generates the file?
What I have:
I have a MYSql database - it is set up through PHPMyAdmin, so I am not familiar enough with the creation process of the database yet. I will look into that.
I can also export the JSON file from PHPMyAdmin, but that is no good to me in a iOS app.
I also have the parsing from a JSON file into an iOS app sorted, but I want to be able to do this on the fly instead of creating potentially hunderds of files.
I hope someone can help me here:-)
I am not necessarily asking for code, but would be mad to ignore it:-)
The problem is that there are not any iOS libraries for directly connecting to a MySQL server; and you really wouldn't want to do that, anyway. So, you need an intermediary server capable of sending data in a format your iOS application can understand. Note, this does not mean the data has to be JSON formatted. But it is very easy to use JSON as the format for your data. Most languages have native support for generating JSON from its native object format(s).
Once you have a server capable of sending data in your preferred format, you need to write some way for your iOS application to retrieve it. You do not have to use a UIWebView for this. As mentioned, the NSURLConnection framework is very easy to use to make such a request. However, there are a lot of other factors to consider when making network requests and others have already done most of the work for you. I like using the AFNetworking framework in conjunction with JSONKit. AFNetworking makes asynchronous calls to remote web services very easy, and JSONKit is nicer than NSJSONSerialization in my opinion.
What I do to retrieve data from MySQL to my iOS app is:
Create a PHP file on your server and prepare it for GET methods (you're going to send data from the iOS app)
Send a request from your iOS app to your php file, like: "www.yourdomain.com/data.php?name=..."
Process the information on your php file and echo the json output.
When connectionDidFinishLoading: convert the NSData to an Array using NSJSONSerialization.
Do whatever you like with the output information
That's just do way I do. I'm not familiar with other approaches.
PHP (and any other server side language) can take the data from the MySQL database and output it to any client as JSON, on the fly. No need to save the JSON to disk beforehand. Of course, from the client's point of view, there really is no fundamental difference (except the data will always be the latest representation of what's in the database).
You also don't have to use a UIWebView. There's a number of ways to make an HTTP request using Objective-C, but you'll likely want to look at something along the lines of NSURLConnection's sendSynchronousRequest:returningResponse:error: method (I prefer using synch methods inside an async block, but that's not the only way). You can find many tutorials on how to do similar things, as well as higher level libraries to simplify the process.

Multiple Questions regarding Iphone and Webserver Communication

Right now I'm in the middle of a project at college, regarding IPhone-Development.
The project consists of a webserver with mysql database and multiple clients (IPhone Devices).
I'm struggling a bit with the basic cocept, which is the communication (receiving, sending data) between IPhone and Webserver.
I've set up an Webserver with a MYSQL-Database. I also have a PHP-Script that accesses the DB and writes the db-Data into an XML-File.
1)What would be the best way to proceed with the Client (IPhone)?
2)Is it possible to directly access a .php File and download the XML to the IPhone?
3) What will be downloaded to the IPhone? The whole XML-File or the XML-Content?
I would then go on to process the XML-File with a XML-Parser (preferably NSXMLParser).
4) How is it possible to store the received data persistently on the IPhone? NSMutableArray? This feature is required for offline mode if any data is added on the IPhone-device.
This brings me to my last question:
5) How do I send data back to the webserver? Or to be more specific, wich datastructure do I have to use to send data back to the webserver? What would be a reasonable way to do this? For example: creating a new XML-file and sending it back to the webserver?
Best Regards,
Alex
Must the data protocol be XML? I'd opt for JSON as the data protocol!
If it's feasible for you to use JSON then take a look at the lightweight SBJson framework. It's going to be much easier to use JSON over XML for iOS, believe me!
To summarize on the other questions, regarding server communication etc.!
Is it a mandatory precondition for you to write the networking code from scratch or are you allowed to use existing open frameworks for that? If you may use a framework then f.e. take a look at MKNetworkKit! It'll take away almost all the hassles that come with writing or incorporating own network code and it has JSON serialization/deserialization build in already!
If not then go with NSURLConnection, NSURLResponse and probably some NSOperation/NSOperationQueue for asynchronous handling and have some fun! :-)
And don't forget the Reachability.h/m from Apple! ;-)
For data persistence on the device either go with CoreData, which is very powerful but mybe (regarding complexity) to much of overhead for your purpose. Or just store your stuff with/in NSUserDefaults!

Android remote MySQL operations using a web service with php

I read some nice articles about how to connect to a remote MySQL database via Android.
Found some really interesting links here and here.
So the common way for getting data seems to be using some kind of webservice (interface, in this case a php script) which queries the db and renders the result in JSON (or XML) format. Then its possible to parse this output with the android JSON_Object implementation. So far so good.
Receiving data from the database and showing it up in a android listview was done in about minutes.
But what is the best practice for writing (inserting) data into tables?
Should a webservice be used here too? (or rather direct mysql conn)
What is the best method to push data to a webservice? (for ex. to insert a new entity in a database) and which format should be used?
In this case I do not use any html forms or anything to post the parameters. So how to post these parameters to the php script? (from within the android app!)
Of course this operation should be secure as well. Implementing a data manipulation machanism is bit more risky (in order to keep the db persistant)
I think, that many apps use some kind of DB, to synchronize data (ex: highscores).
So there should be a best practise for that.
I would recommend keeping anything database-specific hidden behind a web service.
If you build a dependency on MySQL into your application and later find that you need to change databases, the entire installed base has to be cut over. Think about the logistics of accomplishing that for a few minutes and you'll start to realize it's a nightmare.
Premiumsoft's Navicat for MySQL comes with a HTTP tunnel (PHP script) you might be able to use. It basically provides a method for doing anything to a MySQL database over HTTP.
I'd just make sure there are no licensing issues if you plan to distribute your app.

PHP MySQL Website to External SQL System Integration

I am after opinions from some expert web developers as to the best solution to my problem.
THE SETUP
In my job, I look after an online shop, which is based upon osCommerce, with many additional features and tweaks. This is on Linux shared hosting and uses PHP and MySQL as its main technologies.
We are going through the process of upgrading our stock and order system, which is Microsoft SQL based and runs on a data server on our network (a Dell Server PC).
THE PROBLEM
I would like to achieve better integration between the stock/order system and our website, such as to get up-to-date stock information, check prices, etc. With the eventual aim of getting customer data linked as well.
MY POSSIBLE SOLUTION
I currently have XAMPP running on our SBS server for development, which I use to test new code, before uploading to the live site.
I have written a PHP file on this server, which connects to the SQL server and runs various SQL queries, taking in parameters in the $_GET array, retrieving the result as an associative array, JSON encoding the result and echoing the encoded string.
The website just uses something like this to get and use the result:
$result = file_get_contents('http://SBS-SERVER-IP/getinfo.php?partenquiry=' . $cleanStringPartNumber);
if ($result) $stock_info = (array) json_decode($result);
I thought this would be a decent solution as any logins for the SQL, even the fact that it is SQL is not exposed to the website, so if the website was compromised, it shouldn't compromise our system.
I will be making sure that the login for the SQL server only has access to SELECT data as I don't want to update/insert/delete anything as it may cause problems with the stock/order system.
Also, I was thinking of caching the results somewhere, so our stock/order system doesn't suffer performance issues.
I am sure there are many ways of passing the data between the 2 systems, but I want to be sure I am using a solution that is secure, using the most efficient and industry-standard methods of carrying this out.
I see technologies such as cURL, SOAP, XML, JSON, etc and wonder if any of these are ideal.
What are your thoughts?
I would use https if possible to have a secure communication between both servers.
Which technologie you use is up to you and the framework you are willing to learn.
JSON is one of the easiest way to send data from a to b.
SOAP practicly is XML but you don't have to bother with the XML itself.
Using SOAP you can send and store Objects.
Using php's serialize and unserialize you can transform objects too and send them and afterwards store the content of that object into the database.
It's more about preference and productionspeed.
Learning a new framework takes some time at first but can make you more productive afterwards.

Categories