从数据库定义表的所有属性

时间:2014-12-01 10:52:33

标签: php database web-services wampserver

我正在开发一个带有一个参数的Web服务,该参数是表的名称。在获取请求时是否有任何方法可以确定我的表的所有属性。

 <?php
 // Call Address  URL: url: http://localhost/test1/new.php?tab=Request_Table -> GET
 $response = array(); //array for JSON response
 if(isset($_GET["tab"])){ //check for post data
 $tab = $_GET['tab'];
 $element = array();
 if(!empty($tab)){
 $result = mysql_query("SELECT * FROM '$tab'");
 if(!empty($result)) { //check for empty result
 while($data = mysql_fetch_array($result)){ // Fetching results
 extract($data);
 $element["attribute1"] = $data["attribute1"];
 $element["attribute2"] = $data["attribute2"];
 } 
 $response = array("success" => 1, "element" => $element);
 echo json_encode($response); // echoing JSON response
 }else{ //not element found
 $response["success"] = 0;
 $response["message"] = "No element found";
 echo json_encode($response);
  }
  }else { //not element found
  $response["success"] = 0;
  $response["message"] = "No element found";
  echo json_encode($response);
  }
  }else{ // required field is missing
  $response["success"] = 0;
  $response["message"] = "Required field(s) is missing";
  }
  ?>

1 个答案:

答案 0 :(得分:1)

<?php
// Call Address  URL: url: http://localhost/test1/new.php?tab=Request_Table -> GET
$response = array(); //array for JSON response
if(isset($_GET["tab"])){ //check for post data
    $tab = $_GET['tab'];
    $element = array();
    if(!empty($tab)){
        $result = mysql_query("SELECT * FROM $tab");
        if(!empty($result)) { //check for empty result
            $data = array();
            while($row = mysql_fetch_array($result)){ // Fetching results
                $data[] = $row;
            }
            $response = array("success" => 1, "element" => $data);
        }else{ //not element found
            $response["success"] = 0;
            $response["message"] = "No element found";
        }
    }else { //not element found
        $response["success"] = 0;
        $response["message"] = "No valid table name";
    }
}else{ // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
}
echo json_encode($response); // echoing JSON response
?>