1

Good Day Everyone!

I am working on something that is new to me and that is to get data from my phpmyadmin and display it in an android application using XAMPP and JSON. I was wondering what could be the steps that must be taken to make this connection possible. For now, I have a php file that inserts data into a database stored in phpMyAdmin and it works. I am looking to display or add data from an android application. Could somebody please give me steps on how to start this or a link that will help me with this issue. Thanks. Here's my php file right now.

<?php

$server = 'localhost';
$user = 'root';
$password = '';
$database = 'testdb';
$productName = $_POST["txtProductName"];
$price = $_POST["txtPrice"];
$db = mysql_connect($server, $user, $password);



$db_select = mysql_select_db ($database, $db);
$query = "Insert into products (name, price) 
Value ('$productName' , '$price')";
    if (!$db) {
        die('Could not connect to database');
    }   

    enter code here
    if (!mysql_select_db($database, $db))
        die("Could not open products database");
    if (!($result = mysql_query($query , $db)))
    {
        print("Could not execute query<br/>");
        die(mysql_error());
    }

mysql_close($db);       


?>
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
paopao33
  • 107
  • 4
  • 13
  • 1
    http://stackoverflow.com/questions/2938502/sending-post-data-in-android just send form data to the web server like you would do on a webpage, don't try to connect to the database directly from the phone. – rich green Feb 25 '15 at 22:13

1 Answers1

0

I recommend that you just create a few php scripts to provide basic CRUD operation and simply called those php scripts from the Android by sending the proper HTTP request to the server for the CRUD operation you want.

Excellent tutorial that explain this concept is available here

  • Thank you for your suggestion. I was wondering where you put your php files that holds the CRUD operation. I use XAMPP as my web server and I all know right now is i put this specific line http: //10.0.2.2/insert_product.php. Do I have to change something from this specific line. I had an error-free code, but it does not quite work well. It does not enter the data into the database just yet, there are no errors. That line I just put is I think the reason it's holding me back, do you have any suggestions? – paopao33 Feb 26 '15 at 05:54
  • Sorry but I am not sure what you are asking here. For sake of simplicity it might be easier for you to test your php scripts simply by using a web browser and once the php scripts are working properly then build the Android application. That way you will know for sure that the issue is with your android application versus your php script. – user2553585 Feb 26 '15 at 13:23
  • Sorry, what I meant to ask was where should I put the php files in my computer, does it require any specific location in order for the php files to work? – paopao33 Feb 26 '15 at 15:31
  • Assuming that you are using XAMPP default configuration all your web pages including any php scripts should go under the XAMPP root directory in the folder called htdocs or any subdirectory of that folder. Web pages and php scripts all go under the same root directory. – user2553585 Feb 26 '15 at 17:27