从mysql中检索多行

时间:2015-12-17 05:52:17

标签: android mysql

您好我正在尝试从数据库中检索多行数据,但每当我运行我的应用程序时,应用程序崩溃。

就我而言,我想从数据库中检索用户名称的行。

   protected String doInBackground(String... args) {    
        try {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("name", name));
            // getting JSON string from URL
            JSONObject json = jsonParser.
                    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
                coupons = json.getJSONArray(TAG_COUPONS);

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

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

这是我的日志猫

 E/JSON Parser﹕ Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

 3961-4003/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$LoadAllCoupons.doInBackground(CouponPageActivity.java:98)
            at info.androidhive.loginandregistration.CouponPageActivity$LoadAllCoupons.doInBackground(CouponPageActivity.java:71)


            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            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)

这是我的php代码

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

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $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"];


    // push single product into final response array
    array_push($response["products"], $product);
}
// success
$response["success"] = 1;

1 个答案:

答案 0 :(得分:2)

你可能会尝试这会帮助你。

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

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

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


// push single product into final response array
array_push($response["products"], $product);
}
// success
$response["success"] = 1;