使用PHP和MYSQL(在android应用程序中)从数据库中搜索和检索数据


Search and retrieve data from Database using PHP and MY SQL(In android application)

Iam正在开发一个购物应用程序,其中项目以滚动视图显示。

点击其中一个项目时,必须使用必需的字段,使用PHP和MYSQL在数据库中搜索它,并在应用程序中显示确切的产品。

问题:

我可以将值传递到数据库,如果我在数据库中搜索并显示它,则值返回null。

请找到Java类和php文件。

FullImage.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 ",""));