Callum Jenkins
Callum Jenkins

Reputation: 103

Checking if values in Android array exist in MySQL column with PHP, returning records if so

I'm new to PHP and I have been researching how to send and receive data from Android to MySQL using a PHP web layer. I had previously prepared my code to create the Java string arrays emailAddresses[] and phoneNumbers[] which respectively contain all of the email addresses and phone numbers of the phone's contacts.

I want to check each of these values against a MySQL table DATA which contains the fields DATA_TYPE and DATA_CONTENT, which respectively identifies the data as a phone number/email address/other item, and contains the data. I think it best if I load the arrays into PHP and then loop through each of their values to see if a query returns a record. If it does, then I want to build all of the other data in DATA associated to the user with that phone number/email address into a JSON response for Android, which it will later use to copy the data into its own SQLite DB.

I am unsure of how best to handle the input and output for PHP here though. Most examples I see involve setting NameValuePairs as the entity with HTTPPOST, but the examples also seem only to use these as parameters for identifying or copying data from a single record, e.g. for logging in, whereas I want to pass lots of data of the same type (email/phone) for identifying lots of records. I thought it might be a case of specifying the keys as "email[1]","[email protected]" "email[2]","[email protected]" but as I am new to PHP, I wasn't sure how efficient that would be, especially with 100 or so addresses. I'd appreciate some guidance in my new furore, thanks.

Upvotes: 0

Views: 369

Answers (1)

Aleks G
Aleks G

Reputation: 57326

First of all, if you need to pass a lot of data, regardless of the format, it will be slow. How slow is "too slow" is up to your specific application and its requirements. You should do all the HTTP communication in an AsyncTask or another non-UI thread to keep the UI responsive. Displaying a progress dialog, even an indeterminate, would be a nice way of showing to the user that something is going on.

Now, for your specific question. I would suggest creating a JSON array of your Strings to check. This will essentially give you one String containing all the data. Set this string as the value for a NameValuePair with whatever name you want and pass this NameValuePair as the HTTP Post entity to your server. Then in your PHP, you can simply do: $valuesToCheck = json_decode($_POST['name']) - and you'll have an array in your $valuesToCheck variable.

Now you can do whatever you need to do in PHP and get your data in PHP, for example, you can end up with an array of arrays, say $dataToSend would be an array where each element would be the result of calling mysql_fetch_assoc - or any other way. Once you have this data, simply do this in PHP:

header("Content-type: text/json");
echo json_encode($dataToSend);

This will send all your data back to your calling code (Android in this case). Finally, in Android, get the response and JSON-decoded it. If you got back an empty array, then there were no matches in the database. If the array is not empty, then you got your data and do whatever you need with it.

There are plenty blogs/tutorials/etc. on the web about encoding/decoding JSON in Android. For example, you can refer to this one.

Upvotes: 1

Related Questions