1. Are you ready for the Galaxy S20? Here is everything we know so far!

How do I connect my android studio app to a remote SQL database (xampp))?

Discussion in 'Android Development' started by denerex, Oct 21, 2020.

  1. denerex

    denerex Lurker
    Thread Starter

    Hello,

    I am creating an app that finds a piece of username data after a click of a button.

    So far I have:

    >>Installed and configured xampp
    >>Portforwarded my router
    >>set up an online webserver
    >>Used PHP to create a database and implement it into the webserver.

    Php code index.php
    Code (Text):
    1. <?php
    2. echo "<h1>Hello World</h1>";
    3.  
    4. $servername = "localhost";
    5. $username = "root";
    6. $password = "";
    7. $db = "testDatabase";                
    8. $table = "myGuests";      
    9.  
    10. $conn = new mysqli($servername, $username, $password);
    11.  
    12. if ($conn->connect_error) {
    13.   die("Connection failed: <br>" . $conn->connect_error);
    14. }
    15. else {
    16.     echo "Connected to MySQL successfully <br>";
    17. }
    18.  
    19. $sql = "CREATE DATABASE $db";
    20. if ($conn->query($sql) === TRUE) {
    21.   echo "Database created successfully<br>";
    22. } else {
    23.   echo "Error creating database: <br>" . $conn->error;
    24. }
    25.  
    26.  
    27. $conn = new mysqli($servername, $username, $password, $db);
    28. if ($conn->connect_error) {
    29.   die("Connection failed: " . $conn->connect_error);
    30. }
    31.  
    32. $sql = "CREATE TABLE $table (
    33.                     id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    34.                     usernameOut VARCHAR(50) NOT NULL,
    35.                     passwordOut VARCHAR(50) NOT NULL,
    36.                     reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    37.                     )";
    38.  
    39. if ($conn->query($sql) === TRUE) {
    40.   echo "Table MyGuests created successfully<br>";
    41. } else {
    42.   echo "Error creating table: <br>" . $conn->error;
    43. }
    44.  
    45. $sql = "INSERT INTO $table (usernameOut, passwordOut)
    46. VALUES ('Admin', '1-password')";
    47.  
    48. if ($conn->query($sql) === TRUE) {
    49.   echo "New record created successfully<br>";
    50. } else {
    51.   echo "Error: " . $sql . "<br>" . $conn->error;
    52. }
    53.  
    54. $sql = "SELECT id, usernameOut, passwordOut FROM MyGuests";
    55. $result = $conn->query($sql); // $result will be an array
    56.  
    57. if ($result->num_rows > 0) {
    58.   while($row = $result->fetch_assoc()) {
    59.   }
    60. } else {
    61.   echo "0 results";
    62. }
    63.  
    64. $sql = 'DROP DATABASE testDatabase';
    65. if ($conn->query($sql) === TRUE) {
    66.   echo "Database deleted successfully<br>";
    67. } else {
    68.   echo "Error deleting database:<br> " . $conn->error;
    69. }
    70. $conn->close();
    71.  
    72. echo "The time is " . date("h:i:sa");
    73. ?>
    74.  
    I ran my public webpage and I know for sure the php code works and adds the data to the webserver. I am not sure how to make android studio grab the database information from the public website.
    (https//PUBLICIPADRESS:8080)

    Any links or tutorials on how to do this would be appreciated, Thanks!

    - Denver
     


Loading...
Similar Threads - connect android studio
  1. AndiDroid2
    Replies:
    1
    Views:
    313
  2. Ambient Psychosis
    Replies:
    0
    Views:
    316
  3. Kayem59
    Replies:
    4
    Views:
    448
  4. saran ks
    Replies:
    0
    Views:
    694
  5. Poak
    Replies:
    1
    Views:
    791
  6. wxguy
    Replies:
    0
    Views:
    625
  7. Odd Rain
    Replies:
    3
    Views:
    1,085
  8. Chris Vail
    Replies:
    0
    Views:
    392
  9. Daniel Poh
    Replies:
    7
    Views:
    1,249
  10. mdsousa
    Replies:
    2
    Views:
    1,353

Share This Page

Loading...