gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:38:12 28 4
gpt4 key购买 nike

我是 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 个条目),解析为一个字符串,并显示为一个 ListView ,但在 10MB 时它会崩溃并且必须是 fc。如果我能弄清楚如何指定 PHP 和 Android 发挥良好的作用,例如当有人搜索鸡蛋时,则会返回鸡蛋的所有值(碳水化合物、卡路里、毫米等)。

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

打包 carbcounter.project;

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

?>

关于php - 从 MySQL 数据库中检索搜索到的数据并将其返回给用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11291163/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com