2010-09-15 39 views
0

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

,我创建至今的代码下方:

<?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

任何人有想法了? – JavaNoob 2010-09-15 08:42:18

回答

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 culumns information 
$result = mysql_query("SHOW COLUMNS FROM users"); 
if (!$result) { 
    echo 'Could not run query: ' . mysql_error(); 
    exit; 
} 

// Scan through all the fields 
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>"; 
} 
?> 
+0

Yup感谢你的代码你的代码更整洁,更短! – JavaNoob 2010-09-17 02:30:35

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

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

+0

尝试这一个..这是你的代码..我想我修复它..但我不知道如何使用这一个:) – 2015-07-06 14:46:10

+2

你是如何解决它?你从原始代码中改变了什么?从你的答案看,根本不清楚... – Marki555 2015-07-06 14:49:13

+0

这个代码我修正..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; – 2015-07-10 07:12:57