如何使用php web服务在mysql数据库中获取数据

时间:2014-06-07 00:34:06

标签: php mysql web service

我刚开始使用php webservice.I想要获取' urun'中的所有数据。表只是发送' id'这是我的代码,我不知道如何解决它。

if($_GET["function"] == "getUrun"){

    if(isset($_GET["id"]) && $_GET["id"] != ""){
        $id = $_GET["id"];
        $kategoriid =$_GET["kategoriid"];
        $urunadi =$_GET["urunadi"];
        $urunfiyati =$_GET["urunfiyati"];
        $aciklama =$_GET["aciklama"];
        $where="";
        $where = "id='".$id."' AND kategoriid='".$kategoriid."' AND urunadi='".$urunadi."' AND urunfiyati='".$urunfiyati."' AND aciklama='".$aciklama."'";
        $result  = mysql_query("SELECT * FROM urun WHERE ".$where."");
        $rows = array();
        if($r=mysql_fetch_assoc($result))
            {
                $rows[] = $result;
                $data = json_encode($rows);

                echo "{ \"status\":\"OK\", \"getUrun\": ".$data." }";

            }else{
            echo "{ \"status\":\"ERR: Something wrong hepsi\"}";}
    }else{
        echo "{ \"status\":\"ERR: Something wrongs hepsi\"}";}
}   

1 个答案:

答案 0 :(得分:0)

应该是:

if ($result) {
    $rows = array();
    while ($r = mysql_fetch_assoc($result)) {
        $rows[] = $r;
    }
    echo json_encode(array('status' => 'OK', 'getUrun' => $rows));
} else {
    echo json_encode(array('status' => 'ERR: Something wrong hepsi'));
}

您需要在数组中获取所有结果,然后对整个事物进行编码。您还应该使用json_encode作为包含对象,不要尝试手动创建JSON。