用参数解析json

时间:2014-03-20 17:01:13

标签: php json

嗨,我有这个网络服务,我想用afnetworking参数调用它

<?php
header('Content-Type: application/json; charset=utf-8');
include('settings.php');
mysql_connect($host,$user,$password);
mysql_select_db($base);
mysql_query('SET CHARACTER SET utf8');
$patient = (int) $_POST['patient'];

$req = "select * from consultation where id_patient= $patient";
$sql=mysql_query($req);
$nn=mysql_num_rows($sql);

$items = array("items" => NULL);

while ($data=mysql_fetch_array($sql))
{
    $items["items"][] = array(
        "id_consultation" => $data['id_consultation'],
        "id_patient" => $data['id_patient'],
        "date_consultation" => $data['date_consultation'],
        "motif_consultation" => $data['motif_consultation'],
        "taille" => $data['taille'],
        "poids" => $data['poids'],
        "tension_arterielle" => $data['tension_arterielle'],
        "pouls" => $data['pouls'],
        "temperature" => $data['temperature'],
        "observation" => $data['observation'],
        "ordonnance" => $data['ordonnance'],

    );


}
    echo json_encode($items,JSON_UNESCAPED_UNICODE);

?>

当我用

打电话时
http://localhost:8888/services/consultation.php?patient=1

它告诉我{“items”:null}任何人都可以帮助我:) thx

1 个答案:

答案 0 :(得分:0)

$items = array("items" => array());

while ($data=mysql_fetch_array($sql))
{
    $item = array(
        "id_consultation" => $data['id_consultation'],
        "id_patient" => $data['id_patient'],
        "date_consultation" => $data['date_consultation'],
        "motif_consultation" => $data['motif_consultation'],
        "taille" => $data['taille'],
        "poids" => $data['poids'],
        "tension_arterielle" => $data['tension_arterielle'],
        "pouls" => $data['pouls'],
        "temperature" => $data['temperature'],
        "observation" => $data['observation'],
        "ordonnance" => $data['ordonnance']
        );

    array_push($items['items'], $item);
}

echo json_encode($items);