2014-06-07 40 views
0

我剛開始使用php webservice.I想在'urun'表中將所有數據發送給'id'這裏是我的代碼,我不知道如何解決它。如何使用php web服務在mysql數據庫中獲取數據

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\"}";} 
} 
+0

你得到的錯誤是什麼? – vlaz

+0

您的查詢是否返回多行?如果是這樣,你需要一個循環來獲取所有的行。當循環完成時,您應該使用'json_encode'來編碼整個結果,而不是每次循環。 – Barmar

+0

是的,我的查詢返回多行,但正如我所說,我剛開始使用php.Could你請發給我如何寫真循環? – BurcuAtalay

回答

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。

+0

非常感謝.solved :) – BurcuAtalay

相關問題