如何使用 php 在 android 的 mysql 数据库中搜索此字符串是不是作为值存在?

Posted

技术标签:

【中文标题】如何使用 php 在 android 的 mysql 数据库中搜索此字符串是不是作为值存在?【英文标题】:How to search whether this string is present as a value in mysql database in android using php?如何使用 php 在 android 的 mysql 数据库中搜索此字符串是否作为值存在? 【发布时间】:2018-09-10 03:34:54 【问题描述】:

这是我的代码文件 对于所有搜索到的字符串,结果都是正确的。我不知道为什么? 这是我在服务器上的 JSON 文件 https://vikasbajpayee.000webhostapp.com/villagesapi.php php代码文件link of php code 请告诉我是否有人知道这里。提前谢谢。

 button.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            if(name!= null && !TextUtils.isEmpty(name)&&!name.equals("null"))
            new AsyncFetch(name).execute();
            else
                Toast.makeText(Checkout.this, "Please Enter Village name", Toast.LENGTH_SHORT).show();
        
    );

private class AsyncFetch extends AsyncTask<String, String, String> 

    ProgressDialog pdLoading = new ProgressDialog(Checkout.this);
    HttpURLConnection conn;
    URL url = null;
    String searchQuery;

    public AsyncFetch(String searchQuery)
        this.searchQuery=searchQuery;
    

    @Override
    protected void onPreExecute()
    
        super.onPreExecute();
        pdLoading.setMessage("\tLoading...");
        pdLoading.setCancelable(false);
        pdLoading.show();

    

    @Override
    protected String doInBackground(String... params) 
        try 

            url = new URL("https://vikasbajpayee.000webhostapp.com/villagesapi.php");

         catch (MalformedURLException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
            return e.toString();
        
        try 

            // Setup HttpURLConnection class to send and receive data from php and mysql
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(READ_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);

            // setDoInput and setDoOutput to true as we send and recieve data
            conn.setDoInput(true);
            conn.setDoOutput(true);

            Uri.Builder builder = new Uri.Builder().appendQueryParameter("searchQuery", searchQuery);
            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();

         catch (IOException e1) 
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return e1.toString();
        

        try 

            int response_code = conn.getResponseCode();

            // Check if successful connection made
            if (response_code == HttpURLConnection.HTTP_OK) 

                // Read data sent from server
                InputStream input = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                StringBuilder result = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) 
                    result.append(line);
                

                // Pass data to onPostExecute method
                return (result.toString());

             else 
                return("Connection error");
            

         catch (IOException e) 
            e.printStackTrace();
            return e.toString();
         finally 
            conn.disconnect();
        


    

    @Override
    protected void onPostExecute(String result) 

        //this method will be running on UI thread
        pdLoading.dismiss();

        pdLoading.dismiss();
        if (result.equals("no rows")) 
            Toast.makeText(Checkout.this, "No Village exist with this name", Toast.LENGTH_LONG).show();
         else 
            //success
            Toast.makeText(Checkout.this, "Success", Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(Checkout.this,FinalCheckout.class);
            intent.putExtra("namedata",name);
            startActivity(intent);
        

    


在 MySQL PHP 数据库中单击按钮后,我正在搜索“名称”我该怎么做所有搜索到的字符串(此处为名称)。它在所有情况下都运行。

【问题讨论】:

android中使用PHP?问题到底出在哪里。 ? 【参考方案1】:

Android 端:

你实际上并没有在寻找任何东西。您只需从特定的 URL 获取数据。每个请求都会为您提供相同的数据。

你必须像这样向 URL 添加一些参数:

example.com/villagesapi.php?name=myvillage

服务器端:

在您的 PHP 脚本中,您必须根据请求的参数生成响应。 你可以这样做:

if(isset($_GET['name'])) 
   //search from database where name = $_GET['name']
   //then echo json_encode(database response array)

当前的问题就在这里,

if (result.equals("no rows")) 
        Toast.makeText(Checkout.this, "No Village exist with this name", Toast.LENGTH_LONG).show();
 

您正在检查响应是否为“无行”,然后显示错误消息。但是你总是得到一个不是“无行”的 JSON 字符串响应。 这就是为什么对于所有请求,你都会得到真实的。

注意:在 Android 中,您不应该使用 .contain() 方法搜索字符串。您应该解析 JSON 字符串以获取名称。

【讨论】:

以上是关于如何使用 php 在 android 的 mysql 数据库中搜索此字符串是不是作为值存在?的主要内容,如果未能解决你的问题,请参考以下文章

ECharts+PHP+MySQ+ Ajax 实现图表绘制

debian9 安装docker 以及在docker中 安装centos 6 安装指定版本的PHP和MySQ

如何在android中使用php添加多个标记

如何在 php 中获取当前位置 lat -long 值以便在 android 中使用

如何解决 PHP 和 MySQL 中的 UTF-8/Unicode 问题 [关闭]

Android 前端如何使用 PHP 后端?