简体   繁体   English

检索特定的数据行

[英]Retrieving specific rows of data

I am trying to retrieve multiple rows of data. 我正在尝试检索多行数据。 However, I am getting a error. 但是,我遇到了错误。

I want to retrieve rows of which the name (under the column name ) = the name of the user. 我想检索其名称(在列名下 )=用户名的行。 Which part of my code do I have to alter? 我必须更改代码的哪一部分?

 protected String doInBackground(String... args) {
        // Building Parameters
        try {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", name));
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_coupons, "GET", params);

        // Check your log cat for JSON reponse
        Log.d("All Products: ", json.toString());


            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
                products = json.getJSONArray(TAG_PRODUCTS);

                // looping through All Products
                for (int i = 0; i < products.length(); i++) {
                    JSONObject c = products.getJSONObject(i);

                    // Storing each json item in variable
                    String couponexpires = c.getString(TAG_COUPONEXPIRES);
                    String coupondetails = c.getString(TAG_COUPONDETAILS);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_COUPONEXPIRES, couponexpires);
                    map.put(TAG_COUPONEXPIRES, coupondetails);

                    // adding HashList to ArrayList
                    couponsList.add(map);

My php code 我的PHP代码

<?php



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


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

// connecting to db
$db = new DB_CONNECT();

if (isset($_GET["name"])) {
    $name= $_GET['name'];


$result = mysql_query("SELECT * FROM coupons WHERE name = '$name'") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
    $response["products"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $product = array();
        $product["couponcreated"] = $row["couponcreated"];
        $product["couponexpires"] = $row["couponexpires"];
        $product["coupondetails"] = $row["coupondetails"];

        array_push($response["products"], $product);
    }
    // success
    $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No products found";

    // echo no users JSON
    echo json_encode($response);
}
?>

My logcat 我的logcat

5202-5251/info.androidhive.loginandregistration E/JSON Parser﹕ Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
12-21 11:24:50.750    5202-5251/info.androidhive.loginandregistration W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0x41b9f700)
12-21 11:24:50.755    5202-5251/info.androidhive.loginandregistration E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:299)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
            at java.util.concurrent.FutureTask.run(FutureTask.java:239)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.NullPointerException
            at info.androidhive.loginandregistration.CouponPageActivity$LoadAllProducts.doInBackground(CouponPageActivity.java:103)
            at info.androidhive.loginandregistration.CouponPageActivity$LoadAllProducts.doInBackground(CouponPageActivity.java:76)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)

There is syntax error in script(if condition not closed). 脚本中有语法错误(如果条件未关闭)。 use below script: 使用以下脚本:

<?php
// array for JSON response
$response = array();
$response["success"] = 0;
$response["message"] = "No products found";

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

// connecting to db
$db = new DB_CONNECT();

if (isset($_GET["name"])) {
    $name = $_GET['name'];


    $result = mysql_query("SELECT * FROM coupons WHERE name = '$name'") or die(mysql_error());

// check for empty result
    if (mysql_num_rows($result) > 0) {
        $response["products"] = array();

        while ($row = mysql_fetch_array($result)) {
            // temp user array
            $product = array();
            $product["couponcreated"] = $row["couponcreated"];
            $product["couponexpires"] = $row["couponexpires"];
            $product["coupondetails"] = $row["coupondetails"];

            array_push($response["products"], $product);
        }
        // success
        $response["success"] = 1;
        $response["message"] = '';
    } else {
        // no products found


        // echo no users JSON

    }
}
echo json_encode($response);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM