douyihuaimao733955
2012-01-19 18:59
浏览 25
已采纳

如何仅从mySql表的指定字段中检索数据?

I want to display a table (in a web page) containing the data from my database, but only want to display the table with fields selected by the user. For example: I have a total of 6 fields in my table(name, email, age, location, gender, occupation). Let's say that the user on my page selects name, email and age. I want a table to be displayed containing all the row entries, but with only the specified fields.

This is what I've written so far but it doesn't seem to be working.

include("init.php");

$fields = $_POST["fields"];
$sorting = trim($_POST["sorting"]);
$filter = trim($_POST["filter"]);

$sql_query = sprintf("select * from applicants where %s order by %s", $filter, $sorting);
$query = mysql_query($sql_query);
$num_rows = mysql_numrows($query);

echo "<table border='1'>";
echo "<tr>";
    foreach($fields as $field) {
        $field = ucfirst($field);
        echo "<th>$field</th>";
    }
echo "</tr>";

for($i = 0; $i < $num_rows; $i++) {
    echo "<tr>";
    while($field = mysql_fetch_field($query)) {
        if(in_array($field->name, $fields)) {
            $data = mysql_result($query, $i, $col);
            echo "<td>$data</td>";
        } else {
            echo "<td>false</td>";
        }
    }
    echo "</tr>";
}

echo "</table>";

图片转代码服务由CSDN问答提供 功能建议

我想显示一个包含我数据库中数据的表(在网页中),但只想显示 包含用户选择的字段的表。 例如:我的表格中共有6个字段(姓名,电子邮件,年龄,地点,性别,职业)。 假设我页面上的用户选择姓名,电子邮件和年龄。 我希望显示一个包含所有行条目的表,但只包含指定的字段。

这是我到目前为止所写的内容,但它似乎不起作用。

  include(“init.php”); 
 
 $ fields = $ _POST [“fields”]; 
 $ sorting = trim($ _ POST [“排序]  “]); 
 $ filter = trim($ _ POST [”filter“]); 
 
 $ sql_query = sprintf(”select * from applicants where%s order by%s“,$ filter,$ sorting);  
 $ query = mysql_query($ sql_query); 
 $ num_rows = mysql_numrows($ query); 
 
echo“&lt; table border ='1'&gt;”; 
echo“&lt; tr&gt;”; 
  foreach($ fields as $ field){
 $ field = ucfirst($ field); 
 echo“&lt; th&gt; $ field&lt; / th&gt;”; 
} 
echo“&lt; / tr&gt;”; \  n 
for($ i = 0; $ i&lt; $ num_rows; $ i ++){
 echo“&lt; tr&gt;”; 
 while($ field = mysql_fetch_field($ query)){
 if(in_array(  $ field-&gt; name,$ fields)){
 $ data = mysql_result($ query,$ i,$ col); 
 echo“&lt; td&gt; $ data&lt; / td&gt;”; 
} else {  
 echo“&lt; td&gt; false&  lt; / td&gt;“; 
} 
} 
 echo”&lt; / tr&gt;“; 
} 
 
echo”&lt; / table&gt;“; 
   \  n 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duande1985 2012-01-19 19:24
    已采纳

    You need to set the <option> with the value of the column name, then use this code:

    include('init.php');
    
    $fields = $_POST['fields'];
    $sorting = trim($_POST['sorting']);
    $filter = trim($_POST['filter']);
    
    function filterFields($field) {
      if (in_array($field, array(/* Here you need to add the fields you allowed to post, to prevent attack */))) {
        return '`' . $field . '`';
      } else {
        return 'NULL';
      }
    }
    
    $escapedFields = array_map('filterFields', $fields);
    
    // Make the query, here we use 'implode' function to join all array with ','
    // Example if we have array('name', 'time'), then the function will return 'name,time'
    $query = mysql_query('SELECT ' . implode(',', $escapedFields) . ' FROM `applicants` WHERE ' . $filter . ' ORDER BY ' . $sorting);
    // If the query return something, then...
    if (mysql_num_rows($query)) {
      echo '<table border='1'>
    <tr>';
      // Here we print the table header.
      for ($i = 0, $fieldsLength = sizeof($field); $i < $fieldsLength; ++$i) {
        echo '<th>' . ucfirst($fields[$i]) . '</th>';
      }
      echo '</tr>';
    
      // Here we print the result.
      while ($result = mysql_fetch_assoc($query)) {
        echo '<tr>';
        $resultKeys = array_keys($result);
        for ($i = 0, $resultKeysLength = sizeof($resultKeys); $i < $resultKeysLength; ++$i) {
          echo '<td>' . $result[$resultKeys[$i]] . '</td>';
        }
        echo '</tr>';
      }
      echo '</table>';
    }
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题