从 PHP 表中自动生成字段名称

发布于 2024-09-19 13:16:17 字数 776 浏览 3 评论 0原文

从 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++;
    }
}
}
?>

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

There is a problem of automatically retrieving field names from a MySQL table. If possible could the name be placed in this format along with the dynamically created text box? :

The codes that I have created so far are located below:

<?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++;
    }
}
}
?>

The Dynamically created text box (according to the number of columns from the table) are working fine but the field names cannot be generated! Can someone please give advice or help on this? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我三岁 2024-09-26 13:16:17

下面的代码将获取表列并生成输入列表。在你的代码中有很多无用的东西。您不需要从用户中选择*...

这是代码

<?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>";
}
?>

Code below will get table columns and generate the list of inputs. In you code you have a lot of things that are useless. You do not need to select * from users...

Here is the code

<?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>";
}
?>
梦一生花开无言 2024-09-26 13:16:17

回答:

<?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>";
    }

?>

Answer:

<?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>";
    }

?>
梦忆晨望 2024-09-26 13:16:17
<?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++;
    }
}
}
?>

<?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++;
    }
}
}
?>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文