1

My dad wants me to make a android and iPhone app for his website , I'm still learning java and am doing the android one first. What i am looking for is how to connect to his sql database so i could have them logon from within the app and then show website content , But not in a webview how would this work ? Could you point me to some tutorials?

Huntaz556
  • 117
  • 1
  • 3
  • 12

2 Answers2

4

You should create a web service and consume a JSON API or some other data interchange format.

Macarse
  • 91,829
  • 44
  • 175
  • 230
0

You should not make the database accable from the internet! This is very dangerous if you are not very familar with server configuration. Transmit instat the data thrue http. E.g. a PHP script in a JSON container.

Look for the mysql_fetch_assoc function and the json-encode function.

See also this simple and insecure example:

<?php
$conn = mysql_connect("localhost", "mysql_user", "mysql_password");

if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}

if (!mysql_select_db("mydbname")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
}

$result = mysql_query("SELECT * FROM sometable");

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

$rows=array();
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
//       then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
    $rows[]=$row;
}

// output in JSON format
echo(json_encode($rows));

mysql_free_result($result);

?>
rekire
  • 47,260
  • 30
  • 167
  • 264
  • Okay that php script makes sense i know a little bit of php but how would i use that in a android app? – Huntaz556 Feb 15 '12 at 16:26
  • You have to download the file and to decode the file. See [this question](http://stackoverflow.com/questions/3578441/example-of-how-to-download-json-from-server) to get an idea how to do this. Also remember that you need the internet permission. – rekire Feb 15 '12 at 16:41