无法检索从MySQL表,该表符合特定&QUOT数据; UID"数据、MySQL、QUOT、UID

2023-09-14 00:04:56 作者:噬心

我想了解更多关于MySQL和使用Java(Android上)来访问和检索我的WAMS服务器上的数据库中的信息。的方式我的应用程序的设置是,它有一个初始的登录界面也抓住了流体是在记录(从不同的表)的用户名,并将其存储的。

I am trying to learn more about MySQL and using Java (on Android) to access and retrieve information from a database on my WAMS server. The way my app is setup is that it has an initial login screen which also grabs the "uid" of the username that's logging in (from a different table) and stores it.

登录时(这是功能性 - 我的设置,显示被检索的用户名和登录用户的UID Toast通知),它进入一个新的屏幕(dashboard.xml),其中有一个TextView场设置,以显示检索数据(从表中张贴)与所存储的UID相关联。下面是桌子,我试图从获取数据:

Upon login (which is functional - I setup a toast notification that displays the retrieved username and uid of the user logging in), it goes to a new screen (dashboard.xml) which has a TextView field setup to display the retrieved data (from table posted below) associated with the stored "uid". Here is the table I am trying to pull data from:

http://db.tt/4izVQuGB

现在,我已经安装了查询我的分贝为与特定的流体相关联行的PHP文件。我已经使用HTML表单测试了这个文件。

Now, I have setup a PHP file that queries my db for rows that are associated with a specific "uid". I have tested this file using an HTML form.

$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("connection error");
mysql_select_db($dbdb)or die("database selection error");

//Retrieve the User ID
$uid = $_POST['uid'];

//Query
$query = mysql_query("SELECT * FROM node WHERE uid='$uid' AND type='goal'");

//store # of rows returned
$num_rows = mysql_num_rows($query);

if ($num_rows >= 1) {
    while($results=mysql_fetch_assoc($query)) {
        //Store the returned data into a variable
        $output = $results;

        //encode the returned data in JSON format
        echo json_encode($output);
    }
    mysql_close();
}

我得到通过的1 UID值测试的PHP文件中的结果是:

The result I get by testing the PHP file using uid value of 1 is:

{"nid":"1","vid":"1","type":"goal","language":"","title":"test","uid":"1","status":"1","created":"1342894493","changed":"1342894493","comment":"2","promote":"1","moderate":"0","sticky":"1","tnid":"0","translate":"0"}

{"nid":"1","vid":"1","type":"goal","language":"","title":"test","uid":"1","status":"1","created":"1342894493","changed":"1342894493","comment":"2","promote":"1","moderate":"0","sticky":"1","tnid":"0","translate":"0"}

{"nid":"2","vid":"2","type":"goal","language":"","title":"test2","uid":"1","status":"1","created":"1342894529","changed":"1342894529","comment":"2","promote":"1","moderate":"0","sticky":"1","tnid":"0","translate":"0"}

{"nid":"2","vid":"2","type":"goal","language":"","title":"test2","uid":"1","status":"1","created":"1342894529","changed":"1342894529","comment":"2","promote":"1","moderate":"0","sticky":"1","tnid":"0","translate":"0"}

{"nid":"5","vid":"5","type":"goal","language":"","title":"run","uid":"1","status":"1","created":"1343506987","changed":"1343506987","comment":"2","promote":"1","moderate":"0","sticky":"1","tnid":"0","translate":"0"}

{"nid":"5","vid":"5","type":"goal","language":"","title":"run","uid":"1","status":"1","created":"1343506987","changed":"1343506987","comment":"2","promote":"1","moderate":"0","sticky":"1","tnid":"0","translate":"0"}

{NID:9,VID:9,类型:目标,语言:,称号:跑了   hills","uid":"1","status":"1","created":"1343604338","changed":"1343605100","comment":"2","promote":"0","moderate":"0","sticky":"0","tnid":"0","translate":"0"}

{"nid":"9","vid":"9","type":"goal","language":"","title":"run to the hills","uid":"1","status":"1","created":"1343604338","changed":"1343605100","comment":"2","promote":"0","moderate":"0","sticky":"0","tnid":"0","translate":"0"}

现在,我已经写了一些安卓code即建立httppost和应该找回我的数据库表中的冠军。我知道这是不对的(很明显,因为它不工作),但我很困惑,下一步该怎么做。

Now, I have written some android code which sets up httppost and is supposed to retrieve the "titles" in my database table. I know it is wrong (obviously since it doesn't work) but I am confused as to what to do next.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Dashboard extends Activity implements OnClickListener {

    // variable declarations
    String uid = "1";

    // create textview to display retrieved data
    TextView display;

    HttpClient httpclient;
    HttpPost httppost;
    HttpResponse httpresponse;
    HttpEntity httpentity;

    ArrayList<NameValuePair> resultArray;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboard);
        display = (TextView) findViewById(R.id.test);

        // initialize HttpClient
        httpclient = new DefaultHttpClient();

        // initialize HttpPost
        httppost = new HttpPost("http://192.168.1.112/android/fetch.php");

        try {
        // Create new List
        List<NameValuePair> resultList = new ArrayList<NameValuePair>();
        resultList.add(new BasicNameValuePair("uid", uid));

        httppost.setEntity(new UrlEncodedFormEntity(resultList));

        httpresponse = httpclient.execute(httppost);

        httpentity = httpresponse.getEntity();

        InputStream instream = entity.getContent();

        try {
                            // store incoming stream in an array
            JSONArray jArray = new JSONArray(streamToString(instream));
            JSONObject jData = null;

            for (int i = 0; i < jArray.length(); i++) {
                jData = jArray.getJSONObject(i);
                String goals = jData.getString("title");
                display.setText(goals);
            }
        //} catch (JSONException e) {
            //Toast.makeText(this, "No entries found", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(this, e.toString(), Toast.LENGTH_LONG)
                    .show();
        }

    } catch (Exception e) {
        e.printStackTrace();
        Notifications error = new Notifications();
        error.userPassErrorDialog();
    }

}

private static String streamToString(InputStream is) {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

public void onClick(View v) {
    // TODO Auto-generated method stub

}

}

我在Android模拟器中进行测试时,出现以下错误:

I get the following error when testing it in the Android emulator:

http://db.tt/2vg9MqYh

任何帮助或建议,将大大AP preciated。

Any help or suggestions will be greatly appreciated.

推荐答案

在你的Andr​​oid应用程序,您希望JSONArray:

In your Android app, you expect a JSONArray:

// store incoming stream in an array
JSONArray jArray = new JSONArray(streamToString(instream));

不过,在你的PHP文件,你只能输出多个独立的JSON对象,而不是一个真正的数组。我想,你应该先,然后收集从数据库中的所有项目的PHP数组连接code和输出它只有一次。

However, in your PHP file you only output multiple separate JSON objects instead of a real array. I think, you should collect all items from the database in an PHP array first and then encode and output it only once.

我的PHP技能是有点生锈,但我希望这一次将工作:

My PHP skills are a bit rusted, but I hope this one will work:

//store # of rows returned
$num_rows = mysql_num_rows($query);

if ($num_rows >= 1) {
    $output = array();

    while($results = mysql_fetch_assoc($query)) {
        // append row to output
        $output[] = results
    }

    mysql_close();  // shouldn't that be outside the if block?

    //encode the returned data in JSON format
    echo json_encode($output);
}

我期望的输出则是这样的(也许不缩进):

I would expect the output then to be like this (maybe without indentation):

[
    {"nid":"1","vid":"1","type":"goal","language":"","title":"test","uid":"1","status":"1","created":"1342894493","changed":"1342894493","comment":"2","promote":"1","moderate":"0","sticky":"1","tnid":"0","translate":"0"},
    {"nid":"2","vid":"2","type":"goal","language":"","title":"test2","uid":"1","status":"1","created":"1342894529","changed":"1342894529","comment":"2","promote":"1","moderate":"0","sticky":"1","tnid":"0","translate":"0"},
    {"nid":"5","vid":"5","type":"goal","language":"","title":"run","uid":"1","status":"1","created":"1343506987","changed":"1343506987","comment":"2","promote":"1","moderate":"0","sticky":"1","tnid":"0","translate":"0"},
    {"nid":"9","vid":"9","type":"goal","language":"","title":"run to the hills","uid":"1","status":"1","created":"1343604338","changed":"1343605100","comment":"2","promote":"0","moderate":"0","sticky":"0","tnid":"0","translate":"0"}
]