gpt4 book ai didi

php - 使用 PHP 和 MY SQL 从数据库中搜索和检索数据(在 android 应用程序中)

转载 作者:行者123 更新时间:2023-11-29 02:52:21 24 4
gpt4 key购买 nike

我正在开发一个以 ScrollView 显示商品的购物应用程序。

单击其中一项时,必须选择必填字段,使用 PHP 和 MY SQL 在数据库中搜索它,并在应用程序中显示确切的产品。

问题:

我能够将值传递给数据库,如果我在数据库中搜索它并显示它,该值返回 null。

请找到Java类和php文件。

全图.java:

public class FullImage extends AppCompatActivity
{
ImageView fullimage;
TextView name, des, price;
private ProgressDialog progressDialog;
String model_grid, name_grid, price_grid, name_full = null, price_full = null, image_full;
ArrayList < RowItem > val_grid_full = new ArrayList < > ();
RowItem row1_grid = new RowItem();
URL url = null;
BufferedReader reader;
String text;
Context context_tab1;
String rate = "Rs ";

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

// get intent data
Intent in = getIntent();
// Selected image id

model_grid = in .getExtras().getString("model");
name_grid = in .getExtras().getString("name");
price_grid = in .getExtras().getString("price");

fullimage = (ImageView) findViewById(R.id.image_full);
name = (TextView) findViewById(R.id.text_name);
des = (TextView) findViewById(R.id.text_des);
price = (TextView) findViewById(R.id.price_full);
new DataFromServer().execute();
}

public class DataFromServer extends AsyncTask <String, Void, String>
{
@Override
protected void onPreExecute()
{
progressDialog = ProgressDialog.show(FullImage.this, "", "Please wait...");
}

@Override
protected String doInBackground(String...params)
{
try
{
url = new URL("http://xxx");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");

conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Host", "xxx.com");

Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("m", model_grid)
.appendQueryParameter("n", name_grid)
.appendQueryParameter("p", price_grid);

String query = builder.build().getEncodedQuery();
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();

conn.connect();
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;

while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}

text = sb.toString();

}
catch (Exception ex)
{
ex.toString();
}
finally
{
try
{
reader.close();
}
catch (Exception ex)
{
ex.toString();
}
}
return text;
}

@Override
protected void onPostExecute(String text)
{
//Close progress dialog
progressDialog.dismiss();

JSONArray ja;

try
{
ja = new JSONArray(text);
int len = ja.length();
for (int i = 0; i < len; i++)
{
JSONObject json_data = ja.getJSONObject(i);

name_full = json_data.getString("name");

image_full = json_data.getString("image");
price_full = json_data.getString("price");

RowItem rData1 = new RowItem();
rData1.setFull_grid_name(name_full);
rData1.setFull_grid_image(image_full);
rData1.setFull_grid_price(price_full);

val_grid_full.add(rData1);
name.setText(name_full);
price.setText(rate + (price_full));
String url = "xxx" + image_full;
Picasso.with(context_tab1).load(url).into(fullimage);
}
}
catch (Exception e)
{
e.toString();
}

// grid_shopping.setAdapter(new GridAdapter_tab1(Grid_tab1.this,val_grid_tab1));
}
}
}

PHP.php:

<?php
include('con.php');
$model = $_POST['m'];
$price = $_POST['p'];
$name = $_POST['n'];
$q=mysql_query("SELECT oc_product_description.name,oc_product.image,oc_product.price,oc_product.model FROM oc_product INNER JOIN oc_product_description ON oc_product.product_id = oc_product_description.product_id WHERE model = '$model' and price = '$price' and name = '$name'");
while($e2=mysql_fetch_array($q)){
$output[]=$e2;
}
print(json_encode($output));
?>

请注意,上面的 sql 查询工作正常。

仅当我在查询中传递 POST 值时它才返回 null。

任何帮助将不胜感激。

最佳答案

感谢大家花时间。

我自己找到了解决方案。实际上,我使用了一个字符串 (Rs) 来查看字符串中的价格,这引起了问题。

我将这些值连同附加的字符串一起传递给服务器。一旦我将字符串替换为空格,它就可以正常工作。

改变自

in.putExtra("price",price_item);

 in.putExtra("price",price_item.replaceAll("Rs ",""));

关于php - 使用 PHP 和 MY SQL 从数据库中搜索和检索数据(在 android 应用程序中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34354627/

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