存在从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++;
}
}
}
?>
动态创建的文本框(根据表中的列数)工作正常,但无法生成字段名称!有人可以就此提出建议或帮助吗?谢谢!
答案 0 :(得分:0)
答案:
<?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>";
}
?>
答案 1 :(得分:0)
下面的代码将获取表列并生成输入列表。在你的代码中,你有很多无用的东西。您不需要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>";
}
?>
答案 2 :(得分:-2)
<?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++;
}
}
}
?>