duanben1909
2016-03-14 07:42
浏览 55

通过php检索sql数据库中的一行并将输出转换为android应用程序中的textView

i am really really new to android programming so pardon me for my total lack of knowledge

as of now, i have a sql database with 3 rows, "name", "contact" and "status"

I would like to retrieve both "status" whenever that matches the query of both a "name" and "contact". After getting the "status" query,I would like to convert the query into a TextView and display it in an android activity.

I still cant find out how to do so despite hours of searching.Thank you. My biggest problem is retrieving the "status" that matches "name" and "contact". Thank you for your help

This is my php script

<?php 
 

 $name = $_GET['name'];
 $contact = $_GET['contact'];


 require_once('Connect.php');
 

 $sql = "SELECT * FROM database WHERE name=$name and contact = $contact";
 

 $r = mysqli_query($con,$sql);
 

 $row = mysqli_fetch_assoc($r);
 $status =$row["status"];
 
 echo "Your Current Status: ".$status;
 
 
 mysqli_close($con);

EDIT:

My editted snippet is this, converted it to an array somehow and it kinda works.

$result = array();
 $row = mysqli_fetch_array($r);
 array_push($result,array(

 "status"=>$row['status'],
 ));
 
 //displaying in json format 
 echo json_encode(array('result'=>$result));

However, I have totally no idea how to use JSON to retrieve the status from the array and convert it to a String (and eventually TextView). Thank you,anyone for helping

</div>

图片转代码服务由CSDN问答提供 功能建议

我对Android编程真的很陌生,所以请原谅我完全缺乏知识 \ n

截至目前,我有一个包含3行的sql数据库,“name”,“contact”和“status”

我想同时检索两个“状态” 匹配“名称”和“联系人”的查询。 在获得“状态”查询后,我想将查询转换为TextView并将其显示在Android活动中。 < 尽管经过几个小时的搜索,我仍然无法知道该怎么做。谢谢。 我最大的问题是找回与“姓名”和“联系方式”匹配的“状态”。 谢谢你的帮助 \ n

这是我的php脚本

 &lt;?php 
 
 
 
 
 
  r 
 $ name = $ _GET ['name']; 
 
 $ contact = $ _GET ['contact']; 
 
 
 
 
 
nen require_once('Connect.php');  
 
 
 
 
 
 $ sql =“SELE  CT * FROM数据库WHERE name = $ name and contact = $ contact“; 
 
 
 
 
 
 $ r = mysqli_query($ con,$ sql); 
 
 
 
 
 
  
 $ row = mysqli_fetch_assoc($ r); 
 
 $ status = $ row [“status”]; 
 
 
 
 echo“您的当前状态:”。$ status; 
 
 \  r 
 
 
 \ mysqli_close($ con);   
 
  
 
  
 
 
 
 

编辑:

我的编辑片段是这样的,以某种方式将其转换为数组,它有点工作。

  $ result = array(); 
 
 $ row = mysqli_fetch_array($ r); 
 
 array_push($ result,array(
 
 
 
 
“n”status“=&gt; $  row ['status'],
 
)); 
 
 
 
 //以json格式显示
 
 echo json_encode(array('result'=&gt; $ result));   
 
  
 
  
 
 
 
 

但是,我完全不知道如何使用JSON从中检索状态 数组并将其转换为String(最终为TextView)。 谢谢,任何人帮助

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqkv0603 2016-03-14 07:57
    已采纳

    Try

      $sql = "SELECT * FROM your_table WHERE name='$name' and contact = '$contact' ";
    

    mind the single quote ' ' around your variables.

    Use mysqli_error() function to find any errors.

    Further make sure you use JSON to communicate with your server and the android client app.

    For that you should have a Class called "JsonParser" insert below method.

        public JSONObject makeHttpRequest(String url, List<NameValuePair> params) {
            try {
    
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
    
                if (params != null) {
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                }
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
    
                is = httpEntity.getContent();
    
            }catch(UnsupportedEncodingException e){
                Log.e(tag + "-> EncodingError", e.toString());
                e.printStackTrace();
            }catch(ClientProtocolException e){
                Log.e(tag + "-> ProtocolError", e.toString());
                e.printStackTrace();
            }catch(IOException e){
                Log.e(tag + "-> IOError", e.toString());
                e.printStackTrace();
        }
    
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
    
            String line = null;
    
            while((line = br.readLine())!= null){
                sb.append(line + "
    ");
            }
            is.close();
            json = sb.toString();
            Log.i(tag, "-> JSON String Buffer Created");
    
        }catch(Exception e){
            Log.e(tag + "-> Buffer Error", "Conversion Error" + e.toString());
        }
    
        try{
            Log.i("tagconvertstr", "["+json+"]");
            jobj = new JSONObject(json);
            Log.i(tag, "-> JSON object Created");
    
        }catch(JSONException e){
            Log.e(tag + "-> JSON Parser", e.toString());
        }
    
        return jobj;
    }
    

    Next initialize your Textview

        private Textview txtviw;
    
        JSONObject jobj = null;
        JsonParser jParser = new JsonParser();
    

    In onCreate()

        txtviw = (Textview ) findViewById(R.id.your_text_view_id);
    

    Then in an AsyncTask class call this php file and get the json object created.

    In doInBackground()

            List<NameValuePair> params = new ArrayList<NameValuePair>();
    
            params.add(new BasicNameValuePair("name", your_name));
            params.add(new BasicNameValuePair("contact", your_contact));
    
            jobj = jParser.makeHttpRequest(YOUR_URL_TO_PHP_FILE, params);
    

    finally in onPostExecute()

        txtviw.setText(jobj.getString("status"));
    

    You can learn more about AsyncTask from google. Further it is a best practice to use AsyncTask wen communicating with databases.

    打赏 评论
  • duaevb1511 2016-03-14 07:56

    There are two obvious errors in your code - you are selecting something from database instead of table. Second one is wrong SQL query for string variables and missing single quotation marks, so this should be correct code:

    <?php 
        $name = $_GET['name'];
        $contact = $_GET['contact'];
        require_once('Connect.php');
        mysqli_select_db($con, 'database') or die(mysqli_error($con));
        $sql = "SELECT * FROM `table` WHERE `name` = '$name' AND `contact` = '$contact'";
        $r = mysqli_query($con, $sql) or die(mysqli_error($con));
        $row = mysqli_fetch_array($r, MYSQL_ASSOC);
        $status = $row["status"];
        echo "Your Current Status: ".$status;
        mysqli_close($con);
     ?>
    

    Last but not least is a good practice to use mysqli_error() function together with any call to your database in order to inspect any errors.

    打赏 评论

相关推荐 更多相似问题