惠!我想知道如何在mysql数据库中显示来自多个表的数据,并使用android ...中的视图列表显示数据。
不幸的是我的php收到了这个错误:
警告:mysqli_query():( 23000/1052):列' empUsername' in where子句在第11行的C:\ xampp \ htdocs \ elba \ TRYreportclockIN.php中是不明确的
警告:mysqli_fetch_array()要求参数1为mysqli_result,第14行的C:\ xampp \ htdocs \ elba \ TRYreportclockIN.php中给出布尔值 {"导致":[]}
情况是我有两张桌子:
-attendancedetail
-attendancedetailclockout
从table attendancedetail我希望此表中的所有数据如下:
-atdId
-atdClockIn
-empid
-empUsername
从表格出勤到最后时刻我只想要数据:
-atdClockOut
这是我的TRYreportclockIN.php:
<?php
$empUsername= $_GET['empUsername'];
require_once('connectDB.php');
$con = new mysqli($server, $user, $pass, $db);
$sql = "SELECT i.*, o.atdClockOut FROM attendancedetail i join attendancedetailclockout o on i.empUsername=o.empUsername WHERE empUsername='$empUsername' ";
$res=mysqli_query($con,$sql);
$result=array();
while($row=mysqli_fetch_array($res))
{
array_push($result,array
('atdId'=>$row[0],
'atdClockIn'=>$row[1],
'empId'=>$row[2],
'empUsername'=>$row[3],
'atdClockOut'=>$row[1]));
}
echo json_encode(array("result"=>$result));
mysqli_close($con);
?>
activity_tryreportclock_in.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".TRYreportclockIn">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView" />
</LinearLayout>
activity_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<TextView
android:id="@+id/atdId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textStyle="bold" />
<TextView
android:id="@+id/atdClockIn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textStyle="bold"/>
<TextView
android:id="@+id/atdClockOut"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textStyle="bold"/>
<TextView
android:id="@+id/empId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textStyle="bold" />
<TextView
android:id="@+id/empUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textStyle="bold" />
</LinearLayout>
TRYreportclockIn.java
package com.atiemt.elba_employee;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
public class TRYreportclockIn extends AppCompatActivity {
String myJSON;
private static final String TAG_RESULTS="result";
private static final String TAG_ATDID = "atdId";
private static final String TAG_ATDCLOCKIN ="atdClockIn";
private static final String TAG_ATDCLOCKOUT = "atdClockOut";
private static final String TAG_EMPID ="empId";
private static final String TAG_EMPUSERNAME ="empUsername";
JSONArray peoples = null;
ArrayList<HashMap<String, String>> personList;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tryreportclock_in);
list = (ListView) findViewById(R.id.listView);
personList = new ArrayList<HashMap<String,String>>();
getData();
}
protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String atdId = c.getString(TAG_ATDID);
String atdClockIn = c.getString(TAG_ATDCLOCKIN);
String atdClockOut = c.getString(TAG_ATDCLOCKOUT);
String empId = c.getString(TAG_EMPID);
String empUsername = c.getString(TAG_EMPUSERNAME);
HashMap<String,String> persons = new HashMap<String,String>();
persons.put(TAG_ATDID,atdId);
persons.put(TAG_ATDCLOCKIN,atdClockIn);
persons.put(TAG_ATDCLOCKOUT,atdClockOut);
persons.put(TAG_EMPID,empId);
persons.put(TAG_EMPUSERNAME,empUsername);
personList.add(persons);
}
ListAdapter adapter = new SimpleAdapter(
TRYreportclockIn.this, personList, R.layout.activity_list_item,
new String[]{TAG_ATDID,TAG_ATDCLOCKIN,TAG_ATDCLOCKOUT,TAG_EMPID,TAG_EMPUSERNAME},
new int[]{R.id.atdId, R.id.atdClockIn, R.id.atdClockOut, R.id.empId, R.id.empUsername}
);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://192.168.1.22/elba/TRYreportclockIN.php?empUsername=");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
@Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
}
我对android studio和php中的编码不太确定......
答案 0 :(得分:0)
您遇到的问题是在不同的表中有两个具有相同名称的列,并且当您使用联合查询时,MySQL不知道要使用哪个列。
你必须在where子句中使用点符号,就像在连接
中一样SELECT i.*, o.atdClockOut FROM attendancedetail i join attendancedetailclockout o on i.empUsername=o.empUsername WHERE o.empUsername='$empUsername'
注意:您的代码容易受到SQL注入攻击:必须验证用户输入,然后才能在查询中使用它并且您应该使用预准备语句。见How can I prevent SQL injection in PHP?