How to connect to remote MYSQL database via php

18.2k views Asked by At

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";  
}  
?>
2

There are 2 answers

2
Harinder Singh On

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");
5
tinonetic On

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)