2016-08-13 60 views
0

我編寫了一個程序,希望從數據庫中讀取一些數據,它顯示了我在數據庫中創建的星期六表中存在的所有數據,但是我希望使用子句來顯示一些數據數據。實際上,在這個程序中,我想顯示數據只是爲每個人登錄,他的名字是在我稱之爲whoo的字符串中(看看android代碼); 如何才能將其更改爲只讀取與登錄人員相關的數據。 在我的表是一個行,我稱之爲user_name,並在android代碼我有一個字符串,稱爲whoo,我想顯示數據與此條款: WHERE user_name ='$ username' 請幫我... 我寫這個程序使用PHP和JSON 這裏是我的代碼:從json數據庫中讀取數據條款

PHP:

<?php 


$con = mysql_connect("...","...","...."); 

mysql_select_db("...",$con); 

$response=array(); 
$result=mysql_query("select * from saturday"); 

if(mysql_num_rows($result)>0){ 

$response["saturday"]=array(); 

while($row=mysql_fetch_array($result)){ 
    $temp=array(); 
    $temp["username"]=$row["user_name"]; 
    $temp["userside"]=$row["user_sideup"]; 
    $temp["userchair"]=$row["user_chair"]; 
    $temp["userjump"]=$row["user_jump"]; 
    $temp["userstep"]=$row["user_step"]; 
    $temp["usercalf"]=$row["user_calf"]; 



    array_push($response["saturday"],$temp); 


} 

$response["t"]=1; 
echo json_encode($response); 



}else{ 

$response["t"]=0; 
$response["message"]="not fonud"; 
echo json_encode($response); 

} 


?> 

,這是我的android:

import android.app.ListActivity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 

import org.apache.http.NameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 


public class ResultPage extends ListActivity { 

private ProgressDialog pd; 
JSONParser jparser = new JSONParser(); 
ArrayList<HashMap<String, String>> P; 



JSONArray s = null; 
private static String url = "http://akosamanus.xzn.ir/getsaturday.php"; 

TextView who,which; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_result_page); 



    who = (TextView)findViewById(R.id.who); 
    which = (TextView)findViewById(R.id.which); 


    Bundle extras = getIntent().getExtras(); 
    String whoo = extras.getString("uname"); 
    String whiche = extras.getString("uday"); 

    who.setText(whoo); 
    which.setText(whiche); 

    P = new ArrayList<HashMap<String, String>>(); 
    new result().execute(); 
} 





@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    String zname = ((TextView) v.findViewById(R.id.tvsetname)).getText().toString(); 
    String zside = ((TextView) v.findViewById(R.id.tvsetside)).getText().toString(); 
    String zstep = ((TextView) v.findViewById(R.id.tvsetstep)).getText().toString(); 
    String zcalf = ((TextView) v.findViewById(R.id.tvsetcalf)).getText().toString(); 
    String zjump = ((TextView) v.findViewById(R.id.tvsetjump)).getText().toString(); 
    String zchair = ((TextView) v.findViewById(R.id.tvsetchair)).getText().toString(); 
    Intent intent = new Intent(ResultPage.this,CResultPage.class); 

    intent.putExtra("zside",zside); 
    intent.putExtra("zstep",zstep); 
    intent.putExtra("zcalf",zcalf); 
    intent.putExtra("zjump",zjump); 
    intent.putExtra("zchair",zchair); 
    startActivityForResult(intent,1); 


} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK && requestCode==1){ 
     if(data.hasExtra("Result")){ 

     } 
    } 
} 


class result extends AsyncTask<String, String, String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pd = new ProgressDialog(ResultPage.this); 
     pd.setMessage("loading..."); 
     pd.show(); 
    } 


    @Override 
    protected String doInBackground(String... argman) { 



     // ContentValues params = new ContentValues(); 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); // Building Parameters 

     // getting JSON string from URL 
     JSONObject json = jparser.makeHttpRequest(url, "GET", params); 


     try { 
      int t = json.getInt("t"); 

      if (t==1){ 
       s=json.getJSONArray("saturday"); 
       for (int i=0;i<s.length();i++){ 
        JSONObject c=s.getJSONObject(i); 
        String username=c.getString("username"); 
        String userside=c.getString("userside"); 
        String userchair=c.getString("userchair"); 
        String userjump=c.getString("userjump"); 
        String userstep=c.getString("userstep"); 
        String usercalf=c.getString("usercalf"); 
        String today=which.getText().toString(); 


        HashMap<String,String> map=new HashMap<String,String>(); 
        map.put("username",username); 
        map.put("userside",userside); 
        map.put("userchair",userchair); 
        map.put("userjump",userjump); 
        map.put("userstep",userstep); 
        map.put("usercalf", usercalf); 
        map.put("Saturday", today); 


        P.add(map); 



       } 


      }else { 
       Toast.makeText(ResultPage.this, "ooooooooooops", Toast.LENGTH_SHORT).show(); 
      } 


     } catch (JSONException e) { 
      e.printStackTrace(); 

     } 


     return null; 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 
     pd.dismiss(); 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       ListAdapter adapter=new SimpleAdapter(ResultPage.this,P,R.layout.item_list2, 
         new String[]{"username","userside","userchair","userjump","userstep","usercalf","Saturday"}, 
         new int[]{R.id.tvsetname,R.id.tvsetside,R.id.tvsetchair,R.id.tvsetjump,R.id.tvsetstep 
           ,R.id.tvsetcalf,R.id.tvsetday}); 

       setListAdapter(adapter); 




      } 
     }); 

    } 

} 

}


08-14 13:14:11.433 4305-4305/com.example.eagle.start1 E/AndroidRuntime﹕ FATAL EXCEPTION: main 
Process: com.example.eagle.start1, PID: 4305 
android.os.NetworkOnMainThreadException 
     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147) 
     at java.net.InetAddress.lookupHostByName(InetAddress.java:418) 
     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) 
     at java.net.InetAddress.getAllByName(InetAddress.java:215) 
     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142) 
     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169) 
     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124) 
     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365) 
     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560) 
     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492) 
     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470) 
     at com.example.eagle.start1.JSONParser.makeHttpRequest(JSONParser.java:118) 
     at com.example.eagle.start1.ResultPage$result$1.run(ResultPage.java:135) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5254) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

回答

0

對於從數據庫中獲取特定值,我們必須使用GET或POST方法 所以在這裏我已經(使用GET方法)修改你的PHP代碼。

<?php 
    $con = mysql_connect("...","...","...."); 

    mysql_select_db("...",$con); 

    if (isset($_GET["username"])) { 
     $username = $_GET['username']; 

    $response=array(); 
    $result=mysql_query("select * from saturday where user_name= $username"); 

    if(mysql_num_rows($result)>0){ 

    $response["saturday"]=array(); 

    while($row=mysql_fetch_array($result)){ 
     $temp=array(); 
     $temp["username"]=$row["user_name"]; 
     $temp["userside"]=$row["user_sideup"]; 
     $temp["userchair"]=$row["user_chair"]; 
     $temp["userjump"]=$row["user_jump"]; 
     $temp["userstep"]=$row["user_step"]; 
     $temp["usercalf"]=$row["user_calf"]; 



     array_push($response["saturday"],$temp); 


    } 

    $response["t"]=1; 
    echo json_encode($response); 



    }else{ 

    $response["t"]=0; 
    $response["message"]="not fonud"; 
    echo json_encode($response); 

    } 

    } 
    ?> 

而在Android的方面,你必須通過USER_NAME中的NameValuePair像下面

List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("username",who.getText())); 
     final JSONObject json = jParser.makeHttpRequest(url, "GET", params); 

,如果您有任何疑問只是看this或評論如下:) 編碼快樂!

+0

我嘗試你的解決方案,但現在當我打開頁面程序被關閉,並說不幸,start1已經停止! :(((((((( – Dura

+0

你能告訴我錯誤日誌? –

+0

親愛的Vishal Thakkar,我的程序現在運行!!你的解決方案幫助我做到這一點 – Dura