0

how do I make read and write calls to a phpadmin sql database for ios without using webview?

SuperString
  • 21,593
  • 37
  • 91
  • 122
  • Please explain a little bit more. – taskinoor May 11 '11 at 16:32
  • 1
    might be helpful: [1](http://stackoverflow.com/questions/5765855/access-sql-database-through-php) [2](http://stackoverflow.com/questions/71088/what-is-the-best-way-to-access-a-database-from-php) [3](http://stackoverflow.com/questions/2441640/using-php-to-access-a-mysql-database) – sudo rm -rf May 11 '11 at 16:37

2 Answers2

2

Unless your server is insecure, you won't be able to access your database directly. Instead, you'll have to use PHP as a "bridge", of sorts. Here's some sample code to get you started on your PHP script:

<?PHP
$con = mysql_connect("db.something.com","someuser","somepass");
if (!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("db", $con);
$result = mysql_query("SELECT * FROM table");
if(mysql_num_rows($result)) {
    while($something = mysql_fetch_assoc($result)) {
      $somethingmore[] = $something;
    }
  }

header('Content-type: application/json');
echo json_encode($somethingmore);
mysql_close($con);
?>

For me, I needed it in JSON so I formatted the result as JSON. In your application, just make an HTTP request to get the result of the PHP script. For me, it was like this (Using the ASIHTTPRequest library):

NSURL *url = [NSURL URLWithString:@"http://www.test.com/something.php"];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setUsername:@"user"];
[request setPassword:@"pass"];
[request setDownloadProgressDelegate:downloadProgress];
[request setCompletionBlock:^{
    [self dosomething];
}];
[request startAsynchronous];

Then you can parse the data and do stuff appropriately.

sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161
  • where do you put the php script? – SuperString May 11 '11 at 20:26
  • is there a way to hide the php script from public? – SuperString May 11 '11 at 20:56
  • @SuperString: PHP is never public, as it is always run server-side. So, just drop it in your webserver somehwhere. Then you can run it simply by going to http://www.yoursitehere.com/yourfolderhere/yourscripthere.php for example. Even if the public finds the link they won't be able to see your script. – sudo rm -rf May 11 '11 at 23:29
0

I'm writing a custom solution that will utilize an IIS-based WCF Service. This solution will allow you to write a SQL query in your Objective-C code and return a results set of DataTables.

Is there any chance that this would help you do what you need? Would a web service of a different type work better?

mbm29414
  • 11,558
  • 6
  • 56
  • 87