【问题标题】:Auto Generate Field Names from table PHP从表 PHP 中自动生成字段名称
【发布时间】:2011-04-12 13:21:03
【问题描述】:

从 MySQL 表中自动检索字段名称存在问题。如果可能的话,可以将名称与动态创建的文本框一起放置在这种格式中吗? :

到目前为止我创建的代码位于下面:

<?php

include "db_connect.php";

$name = mysql_query("SELECT * from users");

$property = mysql_fetch_field($name);

$i = 0;

$result = mysql_query("SHOW COLUMNS FROM users");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
    while($i<mysql_num_fields($result))
    {
      $meta=mysql_fetch_field($name,$i);
      $new = $meta->name;
      echo "$new: <input type=\"text\" name=\"{$row['Field']}\" size=\"40\"   
      maxlength=\"256\" /><br>";
      $i++;
    }
}
}
?>

动态创建的文本框(根据表中的列数)工作正常,但无法生成字段名称!有人可以就此提供建议或帮助吗?谢谢!

【问题讨论】:

    标签: php mysql dynamic field


    【解决方案1】:

    答案:

    <?php
    
    include "db_connect.php";
    
    $name = mysql_query("SELECT * from checkusers");
    
    $property = mysql_fetch_field($name);
    
    $result = mysql_query("SHOW COLUMNS FROM checkusers");
    if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
    }
        for ($i = 0; $i < mysql_num_fields($name); $i++)
        {
          $meta=mysql_field_name($name, $i);
          echo "$meta: <input type=\"text\" name=\"{$row['health_id']}\" size=\"40\" 
          maxlength=\"256\" /><br>";
        }
    
    ?>
    

    【讨论】:

      【解决方案2】:

      下面的代码将获取表格列并生成输入列表。在您的代码中,您有很多无用的东西。你不需要select * from users...

      这里是代码

      <?php
      include "db_connect.php";
      
      //  This is not needed! Useless query that loads all the info from db
      //$name = mysql_query("SELECT * from users");
      //$i = 0;
      
      //  Here you are getting columns information
      $result = mysql_query("SHOW COLUMNS FROM users");
      
      if (!$result) {
          echo 'Could not run query: ' . mysql_error();
          exit;
      }
      
      //  Scan through all the columns
      while ($field = mysql_fetch_object($result)) {
      
          //  structure of $field looks like this
          /*
            object(stdClass)[1]
            public 'Field' => string 'id' (length=2)
            public 'Type' => string 'int(11)' (length=7)
            public 'Null' => string 'NO' (length=2)
            public 'Key' => string 'PRI' (length=3)
            public 'Default' => null
            public 'Extra' => string 'auto_increment' (length=14)
           */
      
          //
          echo "$field->Field: <input type=\"text\" name=\"$field->Field\" size=\"40\" maxlength=\"256\" /><br>";
      }
      ?>
      

      【讨论】:

        【解决方案3】:

        <?php
        
        include "database.php";
        
        $id = mysql_query("SELECT * from info");
        
        $property = mysql_fetch_field($id);
        
        $i = 0;
        
        $result = mysql_query("SHOW COLUMNS FROM info");
        if (!$result) {
        echo 'Could not run query: ' . mysql_error();
        exit;
        }
        if (mysql_num_rows($result) > 0) {
        while ($row = mysql_fetch_assoc($result)) {
            while($i<mysql_num_fields($result))
            {
              $meta=mysql_fetch_field($result,$i);
              $new = $meta->id;
              echo "$new: <input type=\"text\" id=\"{$row['Field']}\" size=\"40\"   
              maxlength=\"256\" /><br>";
              $i++;
            }
        }
        }
        ?>

        【讨论】:

        • 试试这个..这是你的代码..我想我修好了..但我不知道如何使用这个:)
        • 你是怎么解决的?你从原始代码中改变了什么?从您的回答中根本不清楚...
        • 这是我修复的代码 ..if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_assoc($result)) { while($iid;
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-21
        • 1970-01-01
        • 2022-06-26
        • 2015-11-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多