doujiu8918 2014-06-07 00:34
浏览 31

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

I just start to use php webservice.I want to take all data in 'urun' table just sending 'id' Here is my code and i dont know how to solve it.

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条回答 默认 最新

  • duanci8209 2014-06-07 00:43
    关注

    It should be:

    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'));
    }
    

    You need to get all the results in an array, and then encode the whole thing. You should also use json_encode for the containing object, don't try to create JSON by hand.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法