使用android从mysql中检索数据

时间:2012-12-15 15:16:48

标签: php android mysql

我遇到了一个问题,例如:我们有2个用户,因此用户A将数据插入数据库,可以毫无问题地检索它。用户B将数据插入数据库,当用户B想要检索数据时,用户B获取用户A而非用户B的数据。

导致这个问题的原因是什么?

这是我从mysql数据库获取数据的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();

// check for post data
if (isset($_GET["subject_id"])) {
    $subject_id = $_GET['subject_id'];

    // get a product from products table
    $result = mysql_query("SELECT * FROM subject_offered WHERE subject_id = $subject_id");

    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {

            $result = mysql_fetch_array($result);

            $product = array();
            $product["subject_id"] = $result["subject_id"];
            $product["lecturer_name"] = $result["lecturer_name"];
            $product["time_offered"] = $result["time_offered"];
            $product["subject_details"] = $result["subject_details"];

            //$product["updated_at"] = $result["updated_at"];
            // 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 subject found";

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

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

1 个答案:

答案 0 :(得分:0)

 // get a product from products table
    $result = mysql_query("SELECT * FROM subject_offered WHERE subject_id = $subject_id");

应该是

  // get a product from products table
       $result = mysql_query("SELECT * FROM subject_offered WHERE subject_id = '$subject_id' ");
//some times when you miss '' this single commas then it also create a problem