1

我正在尝试使用行的 ID 从我的数据库表“cond”中读取一行。在这种情况下,我只有一行 ID=1。问题是这段代码总是返回 $result 为空。我已经尝试修复它很长时间了,我也尝试使用 PDO 或 mysqli 来修复它,但我的编码知识真的很有限。如何正确读取行?

这是我的 php 代码:

$response = array();
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();    
if (isset($_GET["pid"])) {
$pid = $_GET['pid'];
$result = mysql_query("SELECT *FROM cond WHERE id = $pid");

if (!empty($result)) {
    if (mysql_num_rows($result) > 0) {

        $result = mysql_fetch_array($result);

        $product = array();
        $product["id"] = $result["id"];
        $product["name"] = $result["name"];
        $product["mon"] = $result["mon"];
        $product["tue"] = $result["tue"];
        $product["wed"] = $result["wed"];
        $product["thu"] = $result["thu"];
        $product["fri"] = $result["fri"];
        $product["sat"] = $result["sat"];
        $product["sun"] = $result["sun"];
        $product["version"]=$result["version"];

        // success
        $response["success"] = 1;

        // user node
        $response["product"] = array();

        array_push($response["product"], $product);

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

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // no product found
    $response["success"] = 0;
    $response["message"] = "No product found.Result=empty";

    // echo no users JSON
    echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

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

这是java代码:

List<NameValuePair> params2 = new ArrayList<NameValuePair>();
params2.add(new BasicNameValuePair("pid", "1")); 
JSONObject json = jsonParser.makeHttpRequest(url_product_detials, "GET", params2);
Log.d("Single Product Details", json.toString());

谢谢!

编辑:使用建议的 PDO

在被建议使用 PDO 后,我上传了下一个代码:

<?php

$id=$_POST["pid"];


try {
require_once 'conf.php';
$conn = mysqlConnector();
$stmt = $conn->prepare('SELECT * FROM cond WHERE id = :id');
$stmt->execute(array('id' => $id));
$result = $stmt->fetchAll();

if ( count($result) ) { 
foreach($result as $row) {
     echo json_encode($result);

}} else {
echo "No rows returned.";
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>

但现在我得到这个错误:

Error parsing data org.json.JSONException: End of input at character 0 of 
threadid=12: thread exiting with uncaught exception (group=0x40cd7930)
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:856)
 Caused by: java.lang.NullPointerException
at com.goout.ListClubs$GetProductDetails.doInBackground(ListClubs.java:456)
at com.goout.ListClubs$GetProductDetails.doInBackground(ListClubs.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
... 4 more
  Activity com.goout.ListClubs has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{410fb410 V.E..... R......D 0,0-480,144} that was originally added here
   android.view.WindowLeaked: Activity com.goout.ListClubs has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{410fb410 V.E..... R......D 0,0-480,144} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:281)
at com.goout.ListClubs$GetProductDetails.onPreExecute(ListClubs.java:435)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at android.os.AsyncTask.execute(AsyncTask.java:534)
at com.goout.ListClubs.onCreate(ListClubs.java:87)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.access$600(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5227)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)

所以我猜php代码是错误的?

4

2 回答 2

2

空响应意味着查询中有错误。

除非明确询问,否则 Mysql 不会告诉您错误是什么。因此,您必须始终检查与服务器交互的每个 mysql 函数的结果,以及结果是否为 FALSE - 检查mysql_error()

此外,您使用的是过时的 mysql 库。退出它并开始使用 PDO,它具有内置的错误报告。

于 2013-08-23T16:01:46.013 回答
0

你说你的数据库名称是 cond。那么你在数据库中的表名是什么?mysql_* 已弃用。尝试以下

$mysqli = new mysqli("localhost", "username", "password", "cond");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if ($result = $mysqli->query("SELECT *FROM table_name WHERE id = $pid")) {

    /* determine number of rows result set */
    $row_cnt = $result->num_rows;

    if ($row_cnt>0)
    {
      while ($row = $result->fetch_row()) 
      {
        $product = array();
        $product["id"] = $result["id"];
        $product["name"] = $result["name"];
        $product["mon"] = $result["mon"];
        $product["tue"] = $result["tue"];
        $product["wed"] = $result["wed"];
        $product["thu"] = $result["thu"];
        $product["fri"] = $result["fri"];
        $product["sat"] = $result["sat"];
        $product["sun"] = $result["sun"];
        $product["version"]=$result["version"];

        // success
        $response["success"] = 1;

        // user node
        $response["product"] = array();

        array_push($response["product"], $product);

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

      // echo no users JSON
      echo json_encode($response);
      /* close result set */
    }
    $result->close();
}

来自http://php.net/manual/en/mysqli-result.num-rows.php

于 2013-08-23T16:06:21.327 回答