Android,MySQL,JSON:MySQL脚本只返回值row date是0000-00-00

时间:2013-08-08 09:50:31

标签: php android mysql json

我正在开发一个Android设备连接到WAMP服务器的系统。 我正在尝试返回一些行,具体取决于用户在Android应用中选择的日期。我必须说日期的默认值是0000-00-00,但这些值可以更新为实际日期。问题是,如果我在设备中选择返回日期为0000-00-00的行,则此方法有效。但是,如果我选择返回某些实际日期(例如2013-08-08)的行,则不起作用(0行受影响)。我认为这是一个格式问题,但日志显示它应该是什么。

我不知道一切都很清楚。

我会复制下面的代码,以防您提供帮助。

PHP(日期称为'fecha')

<?php


// array for JSON response
$response = array();


// include db connect class
require_once __DIR__ . '/db_connect.php';

$db = new DB_CONNECT(); 
$fecha = $_GET['fecha'];
$result = mysql_query("SELECT *FROM resultados2 WHERE fecha = $fecha") or die(mysql_error());


if (mysql_num_rows($result) > 0) {

    $response["resultado"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $resultado = array();
        $resultado["id"] = $row["id"];
        $resultado["nombre"] = $row["nombre"];
        $resultado["tablet"] = $row["tablet"];
        $resultado["fecha"] = $row["fecha"];






        array_push($response["resultado"], $resultado);
    }
    $response["success"] = 1;
    $response["message"] = "Returned";



    echo json_encode($response);
} else {

    $response["success"] = 0;
    $response["message"] = "Error";

    echo json_encode($response);

}
?>

的Android

public class MonitFecha extends ListActivity {
    private ProgressDialog pDialog;

    // Creating JSON Parser object
    JSONParser jsonParser = new JSONParser();
    BaseAdapter adapter;
    ArrayList<HashMap<String, String>> detallesList;

    // url to get all products list
    private static String url_detalles = "http://10.0.0.13/subida/get_fecha_d.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_DNI = "dni";
    private static final String TAG_ID = "id";

    private static final String TAG_NOMBRE = "nombre";
    private static final String TAG_FECHA = "fecha";
    private static final String TAG_TABLET = "tablet";
    private static final String TAG_CORRECTAS = "correctas";
    private static final String TAG_ERRORES = "errores";
    EditText txtName, txtPrice, txtDesc;


    JSONArray resultados = null;
    String fecha;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fecha_candidatos);
        StrictMode.ThreadPolicy policy = new StrictMode. ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);

        boolean isReachable = false;
        try{
            isReachable = InetAddress.getByName("10.0.0.13").isReachable(200);
        } catch (Exception e){
            Log.e("InetAddress", e.getMessage());




        }finally {
            if (isReachable) {
                Intent i = getIntent();

            fecha = i.getStringExtra("fecha");

                new CargarDetalles().execute();



            }else{
                Toast.makeText(getApplicationContext(), R.string.errorserver, Toast.LENGTH_LONG).show();
            }

        }
        setListAdapter(adapter);

        detallesList = new ArrayList<HashMap<String, String>>();

        ListView lv = getListView();


            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    String idd = ((TextView) view.findViewById(R.id.id)).getText()
                            .toString();

                    Intent in = new Intent(getApplicationContext(),
                            MonitDetail2.class);

                    in.putExtra("id", idd);

                    startActivityForResult(in, 100);
                }
            });

        }



    class CargarDetalles extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MonitFecha.this);
            pDialog.setMessage("Actualizando...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }


        protected String doInBackground(String... args) {



            try {
                monitorizar();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();}
            return null;




    }
        protected void onPostExecute(String file_url) {
              pDialog.dismiss();
                runOnUiThread(new Runnable() {
                    public void run() {

                        adapter = new SimpleAdapter(
                                MonitFecha.this, detallesList,
                                R.layout.list_fecha, new String[] {TAG_ID, TAG_NOMBRE, TAG_TABLET, TAG_FECHA},
                                new int[] {R.id.id, R.id.nombre, R.id.tablet, R.id.fecha});

                        adapter.notifyDataSetChanged();
                        setListAdapter(adapter);



                    }
                });

            }

        }
          public void monitorizar() throws Exception{

                try {
                    List<NameValuePair> params = new ArrayList<NameValuePair>();

                    params.add(new BasicNameValuePair("fecha",fecha));
                    JSONObject json = jsonParser.makeHttpRequest(url_detalles, "GET", params);
                   Log.d("params", String.valueOf(params));
                    ArrayList<HashMap<String, String>> temp;
                    temp = new ArrayList<HashMap<String, String>>();


                    int success = json.getInt(TAG_SUCCESS);

                    if (success == 1) {

                        resultados = json.getJSONArray("resultado");

                        for (int i = 0; i < resultados.length(); i++) {

                            JSONObject c = resultados.getJSONObject(i);
                            String id = c.getString(TAG_ID);
                            String nombre = c.getString(TAG_NOMBRE);
                            String tablet = c.getString(TAG_TABLET);
                            String fecha = c.getString(TAG_FECHA);


                            HashMap<String, String> map = new HashMap<String, String>();
                            map.put(TAG_ID, id);
                            map.put(TAG_NOMBRE, nombre);
                            map.put(TAG_TABLET, tablet);
                            map.put(TAG_FECHA, fecha);

                            temp.add(map);
                            detallesList = temp;
                        }
                    } 
                } catch (JSONException e) {
                    e.printStackTrace();
                }






          }

        }

谢谢

1 个答案:

答案 0 :(得分:3)

使用WHERE fecha = '$fecha'(请注意')。

现在,Mysql会将$fecha的值“识别”为“值”,而不是“列名。