php

Parsing UTF-8 encoded content correctly from SQL through Php with Json

SDK Version: 
M3

To get this thing working, just follow these steps:

1. First thing to do is changing the default character set of your SQL database and its tables to UTF-8.

2. Your Php file should look something like this:

  1.   $connection = mysql_connect("server_ip_address", "user", "password");
  2.  
  3.   if (!$connection) die("Couldn't connect to server");
  4.   mysql_select_db("database_name", $connection) or die("Could'nt select the database!");
  5.  
  6.   // This is very IMPORTANT!!!
  7.   mysql_query("SET NAMES 'UTF8'");  
  8.  
  9.   $sth = mysql_query($query);
  10.   $rows = array();
  11.   while($r = mysql_fetch_assoc($sth)) {
  12.       $rows[] = $r;
  13.   }
  14.   print json_encode($rows);
  15.  
  16.   mysql_close($connection);  
  17. }

3. Now you can parse these json forrmatted strings, to your application. Here's how:
http://www.helloandroid.com/tutorials/connecting-mysql-database

Connecting to MySQL database

SDK Version: 
M3
The most spread method to connect to a remote MySQL database from an android device, is to put some kind of service into the middle. Since MySQL is usually used together with PHP, the easiest and most obvious way to write a PHP script to manage the database and run this script using HTTP protocol from the android system. mysql logo

We can code the data in JSON format, between Android and PHP with the easy to use built in JSON functions in both languages.

I present some sample code, which selects data from a database depending on a given condition and creates a log message on the android side with the received data.

Lets suppose that we have a MySQL database named PeopleData, and a table int created, with the following SQL:

  1. CREATE TABLE `people` (
  2. `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,

Syndicate content