douyin4561 2013-08-23 15:56
浏览 95
已采纳

为什么我的mysql查询总是返回空结果?

I'm trying to read one row from my database table "cond" using the ID of the row. In this case I only have one row with the ID=1. The problem is that this code always returns that $result is empty. I've been trying to fix it for a long time and I tried to do it with PDO or mysqli too but my knowledge of coding is really limited. How can I get the read the row properly?

This is my php code:

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

And this is the java code:

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

Thanks!

Edit: Using suggested PDO

After being suggested to use PDO I uploaded the next code:

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

But now I get this error:

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)

So I guess the php code is wrong?

  • 写回答

2条回答 默认 最新

  • doutan1671 2013-08-23 16:01
    关注

    An empty response means there was error in the query.

    Mysql won't tell you what was the error unless asked explicitly. So, you have to always check the result of every mysql function interacting with server and if result is FALSE - check mysql_error().

    Also, you are using outdated mysql library. Quit it and start using PDO, which has built-tin error reporting.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器