dongyidao1461 2014-12-01 10:52
浏览 35
已采纳

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

I'm developing a web service with one parameter which is the name of a table. Is there any method to determine all the attributes of my table while fetching the request.

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

  • douji9518 2014-12-01 10:56
    关注
    <?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
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比