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
<?php

   //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;
   mysql_close();
?>


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("http://10.0.2.2/android_connection/connect_to_database.php");
      
      try {
         response = client.execute(post);
      } catch (ClientProtocolException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
      
      //Display the number retrieved from the server over the total number in the carpark
      displayCars.setText(" /120");
   }


Any help is greatly appreciated.
Crispy91
Freshman
Freshman
 
Posts: 2
Joined: Thu Apr 18, 2013 4:53 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 22 guests