Android - 使用PHP和MySQL获取特定数据库值并将其显示在TextView中

时间:2015-06-05 09:52:02

标签: php android mysql

我正在为小型停车位系统进行数据库连接。

将向用户显示每个停车行的值,然后当他们按下按钮到其中一行时,将开始新的活动。

这是db连接和查询的代码:

<?php

$servername = "kevinmurvie.comxa.com";
$username = "a5299948_kaem";
$password = "xxxxxxx";
$dbname = "a5299948_parkit";

$con=mysqli_connect("$servername","$username","$password","$dbname");

if (mysqli_connect_errno($con))
{
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$slotname = $_GET['slotName'];
$result = mysqli_query($con,"SELECT SLOT_NAME FROM parking_slot where SLOT_NAME='$slotname'");
$row = mysqli_fetch_array($result);
$data = $row[0];

if($data){
echo $data;
}
mysqli_close($con);

?>

对于连接类:

public class DatabaseActivity extends AsyncTask<String,Void,String>{
   private TextView selectedSlot;
   private Context context; 

   public DatabaseActivity(Context context,TextView selectedSlot) {
      this.context = context;
      this.selectedSlot = selectedSlot;
   }

   protected void onPreExecute(){

   }

   @Override
   protected String doInBackground(String... arg0) {
      try{
         String slotName = (String)arg0[0];
         String link = "http://kevinmurvie.comxa.com/parkit.php?slotname="+slotName;

     //URL url = new URL(link);
     HttpClient client = new DefaultHttpClient();
     HttpGet request = new HttpGet();
     request.setURI(new URI(link));
     HttpResponse response = client.execute(request);
     BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

       StringBuffer sb = new StringBuffer("");
       String line="";

       while ((line = in.readLine()) != null) {
          sb.append(line);
          break;
        }
        in.close();
        return sb.toString();
     }

     catch(Exception e){
        return new String("Exception: " + e.getMessage());
     }

  }

@Override
   protected void onPostExecute(String result){
      this.selectedSlot.setText(result);
   }
}

这是指定插槽的按钮的代码:

String selectedSlotA = "SlotA";
String selectedSlotB = "SlotB";
String selectedSlotC = "SlotC";
String selectedSlotD = "SlotD";

TextView parkAvailableA = (TextView) findViewById(R.id.parkASlot);

DatabaseActivity dbA = (DatabaseActivity) new DatabaseActivity(this,parkAvailableA);
dbA.execute(selectedSlotA);

Button buttonA = (Button) findViewById(R.id.parkABtn);

//new DatabaseActivity(this,parkAvailableA).execute(selectedSlotA);
//parkAvaliableA.setText("Available Slot : " + SlotA.available_slot);
buttonA.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        /*if(SlotADbActivity == 0)
        {
            Toast.makeText(getApplicationContext(), 
                    "Parking slot A is full", 
                    Toast.LENGTH_SHORT).show();
        }
        /*
        else
        {
            SlotA.available_slot -= 1;
            db.updateSlot(new ParkingSlot());
        }
        //parkAvaliableA.setText("Available Slot : " + SlotA.available_slot);*/


        //new DatabaseActivity(this,selectedSlot,0).execute(SlotA);

        Intent myIntent = new Intent(MainMap.this, ParkA.class);

        MainMap.this.startActivity(myIntent);
    }

});

问题是我不懂互联网上的教程。我阅读了本教程http://www.tutorialspoint.com/android/android_php_mysql.htm,它用于从数据库中获取多个值,但我无法使我的应用获取并显示特定值。我是否将教程点中的代码搞砸到不起作用的程度?好吧,我的程序可以编译,但它不会显示值。

更新:

我已经更新了代码,我得到了奇怪的错误..

混乱的HTML中有一些混乱的HTML标签“PHP Error”..

enter image description here

0 个答案:

没有答案