如何查询外键并获取相应的行

时间:2014-02-15 05:17:05

标签: php mysql sql foreign-keys

我的PHP查询有问题。我只需要在listview中获得点击患者的处方。

以下是处方的表格结构。

enter image description here

enter image description here

我需要患有身份79的患者才能显示他所有相应的处方。请帮帮我。

这是PHP。

<?php

/*
 * Following code will list all the products
 */

// 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["uid"])) {
    $uid = $_GET['uid'];

// get all products from products table

$result = mysql_query("select * from prescription") or die(mysql_error());

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

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $prescription = array();
        $prescription["pres_id"] = $row["pres_id"];
        $prescription["pres_name"] = $row["pres_name"];
        //$product["price"] = $row["price"];


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

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

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

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

1 个答案:

答案 0 :(得分:1)

要获得所选患者的所有处方,不会是:

mysql_query("select * from prescription where patient_user_fkey = '$patientID'")or die(mysql_error());

而不是:

mysql_query("select * from prescription") or die(mysql_error());