Need help retrieving information from a database on a server

Put your problem here if it does not fit any of the other categories.

Need help retrieving information from a database on a server

Postby Crispy91 » Thu Apr 18, 2013 5:04 pm

Hey, I am currently making an app for a vehicle detection system. The app needs to display the total number of cars detected by this application. I have stored the data taken from the application on a database on a xampp server. The main problem I am having is retrieving the information from the database. I have been following a tutorial about this, but it isn't very clear to me to see how to do this. I have the following php script for connecting to the database and retrieving the information I need.

Code: Select all

   //Parameters for connecting to the database
   $hostname = "localhost";
   $database_name = "itb_carpark";
   $username = "root";
   $password = "";
   //Connection String. If it fails causes error message
   $connection = mysql_connect($hostname,$username,$password) or  trigger_error(mysql_error(),E_USER_ERROR);
   //Access the database
   mysql_select_db($database_name, $connection);
   /*Query to get number of cars and execute the query
    *If there is a problem with the query an error is returned */
   $get_number_of_cars = "SELECT SUM(cars) AS total_number_of_cars FROM carsentered";
   $query_runner = mysql_query($get_number_of_cars) or die(mysql_error());
   //Get the results from the executed query
   $result = mysql_fetch_assoc($query_runner);
   $total = $result['total_number_of_cars']; // I need to display this number in the android app.
   echo "The total is ".$total;

Then here is my connection method in my Android App to access the php script.

Code: Select all
   public void connectToDatabase(){   
      client = new DefaultHttpClient();
      post = new HttpPost("");
      try {
         response = client.execute(post);
      } catch (ClientProtocolException e) {
      } catch (IOException e) {
      //Display the number retrieved from the server over the total number in the carpark
      displayCars.setText(" /120");

Any help is greatly appreciated.
EDIT I just ran the code again and my error logs are actually working now. I can't even establish a connection
to the database. I have tried running this on my actual device and the emulator, with the relevant ip address for each device, but it won't connect. Initially I thought this was to do with xampp. When I entered the url to access phpmyadmin in my browser, I got a security warning to change a config file. I did this and it solved the issue on the browser but still not on the device/emulator. I also entered the url into my phones browser and it worked fine. It's just the app that can't connect.
Posts: 2
Joined: Thu Apr 18, 2013 4:53 pm


Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 13 guests