2017-04-10 12 views
0

這是根據食物和輸入的數量顯示膳食中碳水化合物含量的代碼。 PHP代碼工作正常。當我嘗試獲取輸出時,它顯示空值。 (Carb:null)輸出值作爲NULL返回,而不是存儲在數據庫表中的值

訪問getdata.php

<?php 

if($_SERVER['REQUEST_METHOD']=='GET'){ 

$food = $_GET['food']; 
$quantity = $_GET['quantity']; 

require_once('dbConnect.php'); 

$sql = "SELECT * FROM carbcontent WHERE food='$food' & quantity='$quantity'"; 
$r = mysqli_query($con,$sql); 

$res = mysqli_fetch_array($r); 

$result = array(); 

array_push($result,array(

"carb"=>$res['carb'] 

) 
); 

    echo json_encode(array("result"=>$result)); 

    mysqli_close($con); 

    } 


    dbConnect.php 
    <?php 
    define('HOST','localhost'); 
    define('USER','root'); 
    define('PASS',''); 
    define('DB','slidingscale'); 

    $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect'); 

    Config.java 
    public class Config { 
    public static final String DATA_URL = "http://10.1.56.2 /getData.php?food=&quantity="; 
    public static final String KEY_CARB ="carb"; 
    } 

    activity_main.xml 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:orientation="vertical" 
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" 
android:weightSum="1"> 


<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="FOOD" 
    android:id="@+id/textView" /> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/editText" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="QUANTITY" 
    android:id="@+id/textView2" /> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="number" 
    android:ems="10" 
    android:id="@+id/editText2" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="GET" 
    android:id="@+id/button" /> 
<TextView 
    android:id="@+id/textViewResult" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 
</LinearLayout> 

    MainActivity.java 
    import android.app.ProgressDialog; 
    import android.os.Bundle; 
    import android.support.v7.app.AppCompatActivity; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.widget.TextView; 
    import android.widget.Toast; 

    import com.android.volley.RequestQueue; 
    import com.android.volley.Response; 
    import com.android.volley.VolleyError; 
    import com.android.volley.toolbox.StringRequest; 
    import com.android.volley.toolbox.Volley; 

    import org.json.JSONArray; 
    import org.json.JSONException; 
    import org.json.JSONObject; 

    public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

private EditText editText; 
private EditText editText2; 
private Button button; 
private TextView textViewResult; 

private ProgressDialog loading; 

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

    editText = (EditText) findViewById(R.id.editText); 
    editText2 = (EditText) findViewById(R.id.editText2); 
    button = (Button) findViewById(R.id.button); 
    textViewResult = (TextView) findViewById(R.id.textViewResult); 

    button.setOnClickListener(this); 
} 

private void getData() { 
    String food = editText.getText().toString().trim(); 
    if (food.equals("")) { 
     Toast.makeText(this, "Please enter an food", Toast.LENGTH_LONG).show(); 
     return; 
    } 
    String quantity = editText2.getText().toString().trim(); 
    if (quantity.equals("")) { 
     Toast.makeText(this, "Please enter quantity", Toast.LENGTH_LONG).show(); 
     return; 
    } 
    loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false); 

    String url = Config.DATA_URL+editText.getText().toString().trim()+editText2.getText().toString().trim(); 


    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      loading.dismiss(); 
      showJSON(response); 
     } 
    }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Toast.makeText(MainActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show(); 
       } 
      }); 


    RequestQueue requestQueue = Volley.newRequestQueue(this); 
      requestQueue.add(stringRequest); 

} 

private void showJSON(String response){ 

    String carb=""; 

    try { 
     JSONObject jsonObject = new JSONObject(response); 
     JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY); 
     JSONObject collegeData = result.getJSONObject(0); 

     carb = collegeData.getString(Config.KEY_CARB); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    textViewResult.setText("\nCarb:\t" +carb); 
} 

@Override 
public void onClick(View v) { 
    getData(); 
} 
} 

回答

0

你的問題的直接原因最有可能是你的包裹數量爲字符串,用單引號,而不是把它當作一個數字。如果是的話,那麼下面應該工作:

$sql = "SELECT * FROM carbcontent WHERE food='$food' & quantity=$quantity"; 

但是你應該認真考慮使用準備好的語句,這將釋放你擔心過數據的類型包含在您的查詢。

你有沒有可能忘記選擇數據庫?

$db = mysql_select_db('your_db'); 

使得查詢在PHP代碼之前,將這個。

+0

進行更改後,輸出顯示爲(「Carb:」)未顯示任何值 –

+0

您確定已將數據傳遞到服務器嗎?你確定桌子上有數據嗎? –

+0

是的,先生。 PHP代碼也可以正常工作。 –

相關問題