gpt4 book ai didi

php - 检索特定的数据行

转载 作者:行者123 更新时间:2023-11-30 01:41:52 26 4
gpt4 key购买 nike

我正在尝试检索多行数据。但是,我遇到了一个错误。

我想检索名称(在列名称 下)= 用户名称的行。我必须更改代码的哪一部分?

 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);

我的 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);
}
?>

我的日志

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)

最佳答案

脚本中存在语法错误(如果条件未关闭)。使用以下脚本:

<?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);
?>

关于php - 检索特定的数据行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34388540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com