-2

I have to build an android app which connects to a remote MYSQL webserver and retrieve information for display purpose. So I did research coz I had no idea where to start. I read an article it mentions the better approach for that is to use php script. And didn’t know anything about how server, database and php works, so I studied php from here “http://www.homeandlearn.co.uk/php/php12p1.html” to understand and downloaded WhampServer to do some testing. On local machine everything worked fine. But main thing I don’t understand is “HOW TO CONNECT TO REMOTE SERVER/DATABASE”. It’s obvious that I’m doing something really stupid, I just need help to find out what am I doing wrong. When we test php script on local machine in webrowse we use "localhost/some.php." But when I want to test same php script on remoter server from my local machine then what and how should I do? Do I need to make some changes in configuration file on server side? I have done more research before asking this question here to understand remote server connection in php but I still don’t understand. I have gone through almost all the pages within the link below: https://www.google.co.uk/search?q=connect+to+remote+mysql+webserver+php&rlz=1C1AVSX_enGB447GB448&oq=connect+to+remote+mysql+webserver+php&aqs=chrome.0.69i57j69i62l3.19724j0&sourceid=chrome&ie=UTF-8#fp=5a2359cf96dc5f79&q=how+to+connect+to+remote+mysql+web+server+php

And help would be much appreciated. If my question is not clear please let me know I'll try to explain more. or think of it as if you have to connect to a remote MySQL server how would you do , means what is the process and steps involved in that. Thanks everyone.

Edit

I have created a database "dealt3_raj_test" on remote server. and when I type "examplewebserver.CO.UK/myphpscriptname" in my web browser. It gives me error "An error occurred , You have reached the error page"

<?PHP   
$user_name = "dealt3_raj";  
$password = "5dN5nh&eMd(vCR$dzk";  
$database = "dealt3_raj_test";  
$server = "examplewebserver.CO.UK";  
$db_handle = mysql_connect($server, $user_name, $password);  
$db_found = mysql_select_db($database, $db_handle);  
if($db_handle)    
{         
    print "Connected";  
}  
else  
{  
    print "Can not connect to server";  
}         
if ($db_found)   
{ 
     print "DataBase found";  
}  
else   
{  
    print "DataBase not found";  
}  
?>
user4035
  • 22,508
  • 11
  • 59
  • 94
Raj23
  • 57
  • 2
  • 2
  • 5
  • What code did you tried? And with which result? – Alma Do Aug 26 '13 at 10:39
  • The same way you connect to the local MySQL, just change the host and credentials. Make sure, that remote MySQL accepts connections from your host. – user4035 Aug 26 '13 at 10:42
  • @Alma Do Mundo - I have posted the code , do you think is there any error in the code ? – Raj23 Aug 26 '13 at 11:48
  • 1
    @user2314699 - for `mysql_*` function use `mysql_error()` to show error. In general, don't use `mysql_*` functions - they are deprecated. – Alma Do Aug 26 '13 at 11:54
  • @Alma Do Mundo - Thanks ,after your suggestion i checked here "http://www.php.net/manual/en/function.mysql-connect.php" but my problem is still same . Do i need to put that script on remote server ? – Raj23 Aug 26 '13 at 12:08
  • @user4035 - I have tried that but its give me error please see the edit part. Yeah it dose allow coz i have tested with my c# program and through mysql workbench I can open the connection.I have added "%"(wildcard) in remote mysql connection. – Raj23 Aug 26 '13 at 12:15
  • Hi Guys , that would be nice if you could leave a comment explaining why have you downvoted the question. I didn't ask for code, if you read the question carefully i asked " think of it as if you have to connect to a remote MySQL server how would you do , means what is the process and steps involved in that. " But Thanks anyway for your time . – Raj23 Aug 26 '13 at 12:46
  • @user2314699 I don't see any errors in your code. Try to print mysql_error() after calling mysql_connect and mysql_select_db. Probably, it will clarify, what's wrong. I edited your code a little bit to allow the syntax colouring. – user4035 Aug 26 '13 at 18:04
  • @user4035 - Thanks a lot, now I understand what do you mean in your first comment. And that's what I was looking for. After that ran into MYSQL 4.1 and pre 4.1 password issues but I manage to solve those. You did read my question carefully and your comment was spot on. Thanks again. – Raj23 Aug 27 '13 at 12:04

2 Answers2

2

Adding onto @user4035's comment, after opening the connection, use JDBC in your Android/Java code to interact with the database.

That said, it is not good practice. Rather create a web service

  1. Your application may experience latency/connectivity issues. This will impact performance.
  2. Your MySQL server will have to be open to remote connections which is strongly advised against
  3. If your Android App is intended for public usage, it means the database username and password of your MySQL server reside on everyone's phone using your app. Encrypted or not this makes your database server a "step less" secure

Well answered here on SO (JDBC vs Web Service for Android)

Community
  • 1
  • 1
tinonetic
  • 7,751
  • 11
  • 54
  • 79
  • uhh... what? User4035 never answered this question, and this question isn't about Java. Wrong tab? – John Dvorak Aug 26 '13 at 12:09
  • Wrong. Android IS Java. To quote the asker "...I have to build an android app which connects to a remote MYSQL webserver and retrieve information for display purpose.So I did research coz I had no idea where to start. I read an article it mentions the better approach for that is to use php script..." From my understanding, the PHP solution is secondary if not misunderstood(by him/her) to his/her primary problem of “HOW TO CONNECT TO REMOTE SERVER/DATABASE” – tinonetic Aug 26 '13 at 12:21
  • Still I'm confused about that "Adding onto @user4035's answer" part – John Dvorak Aug 26 '13 at 12:31
  • ah, got it - user4035 posted a comment, not an answer. Edited to clarify and to unlock my vote – John Dvorak Aug 26 '13 at 12:33
  • Thanks. My mistake, user4035 made a comment. Not an answer – tinonetic Aug 26 '13 at 12:41
-1

use this code to connect with data base

    $username = "database user name";
    $password = "DB password";
    $hostname = "hostname";
    //connection to the database
    $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
    //select a database to work with
    $selected = mysql_select_db("data base name",$dbhandle) or die("Could not select examples");
Harinder Singh
  • 319
  • 3
  • 14