从MySQL数据库中检索搜索的数据并将其返回给用户


Retrieve searched data from MySQL database and return it to user

我是Android和PHP的新手。我正在尝试做的是从外部MySql数据库将搜索值检索回Android设备。该数据库由 200,000 个条目组成,大小为 174MB,因此无法将其包含在包中并使用内部 SqlLite。

<?php
  mysql_connect("myhost","myuser","mypass");
  mysql_select_db("mydb");
$sql=mysql_query("select * from myTable");
$output = array();
while($row = mysql_fetch_assoc($sql)) {
$output['txt'][] = $row;
}
exit (json_encode($output));
mysql_close();
?>

在从布局调用的测试类中,我有这个。问题是,我可以连接到外部数据库,甚至可以指定添加到数据库,但我迷失在获取特定的用户定义术语上。现在当我运行它时,它将尝试下载整个表(120,000 个条目),解析为字符串并显示给列表视图,但在 10MB 时它崩溃并且必须是 fc。如果我能弄清楚如何指定 PHP 和 Android 玩得不错,比如当有人搜索鸡蛋时,那么鸡蛋的所有值都会返回(碳水化合物、卡路里、毫米等)。

由于 PHP 脚本是静态的,我不明白如何更改 get 命令,但我知道这是可能的,因为我以前见过应用程序来做到这一点。

包装碳计数器项目;

import java.io.BufferedReader;
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.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SqlTest extends Activity {
/** Called when the activity is first created. */
   TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqltest);      
    LinearLayout rootLayout = new LinearLayout(getApplicationContext());  
    txt = new TextView(getApplicationContext());  
    rootLayout.addView(txt);  
    setContentView(rootLayout);  
    txt.setText("Connecting..."); 
    txt.setText(getServerData(KEY_121)); 

}
public static final String KEY_121 = "http://android.mywebspace.com/androidscript.php";

private String getServerData(String returnString) {
   InputStream is = null;
   String result = "";
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("Abbrev","eggs"));
    try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(KEY_121);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
    }
    catch(Exception e)
    {
            Log.e("log_tag", "Error in http connection "+e.toString());
    }
    try
    {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) 
            {
                    sb.append(line + "'n");
            }
            is.close();
            result=sb.toString();
    }
    catch(Exception e)
    {
            Log.e("log_tag", "Error converting result "+e.toString());
    }
    try
    {
            JSONArray jArray = new JSONArray(result);
            for(int i=0;i<jArray.length();i++){
                    JSONObject json_data = jArray.getJSONObject(i);
                    Log.i("log_tag","id: "+json_data.getInt("id")+
                            ", type: "+json_data.getString("type")+
                            ", description: "+json_data.getInt("description")+
                            ", carbs: "+json_data.getInt("carbs")
                    );
                    returnString += "'n't" + jArray.getJSONObject(i); 
            }
    }
    catch(JSONException e)
    {
            Log.e("log_tag", "Error parsing data "+e.toString());
    }
    return returnString; 
}    
}

提前谢谢。

附言我有正确的登录名、密码、数据库等。我为帖子更改了它们。

您必须在查询中放置一个 WHERE 子句。

像这样捕获从 Android 发送到您的脚本的 Abbrev 参数......

<?php
    $abbrev = mysql_real_escape_string($_POST['Abbrev']);
    $query = sprintf("SELECT * FROM myTable WHERE Abbrev='%s'",$abbrev); //I assumed here that your table field is called Abbrev too
    ... your code here
?>