使用mysql_fetch_row()生成编辑表单

时间:2013-08-15 10:51:46

标签: php mysql rows

我希望得到一个从表中生成编辑表单的函数。 我想将主键转换为隐藏输入,其余转换为文本框。 这是我能得到的

function getform($table)  {
    $html= "";  
    $result = mysql_query("Select * From $table");
    if (!$result) {
       echo 'Could not run query: ' . mysql_error();
       // return from function here. don't know what value you need.
       return "";
    }
    $fieldnames = array();
    $fieldCount = mysql_num_fields($result);
    for( $i=0; $i<$fieldCount; $i++ ) {
       $fieldnames[] = ucfirst(mysql_field_name( $result , $i ));
    }

);
}

我无法弄清楚如何获得主键,其次是如何获得主场的大小

2 个答案:

答案 0 :(得分:0)

使用“DESCRIBE table”(其中'table'是db表的名称)来获取表的描述,这将为您提供如下所示的数组。通过这个数组递减(用户表,在我的例子中)将为您提供有关该表所需的所有信息。

Array
(
    [user] => Array
        (
            [0] => Array
                (
                    [Field] => id
                    [Type] => int(11)
                    [Null] => NO
                    [Key] => PRI
                    [Default] => 
                    [Extra] => auto_increment
                )

            [1] => Array
                (
                    [Field] => created
                    [Type] => int(11)
                    [Null] => NO
                    [Key] => 
                    [Default] => 
                    [Extra] => 
                )

            [2] => Array
                (
                    [Field] => updated
                    [Type] => int(11)
                    [Null] => NO
                    [Key] => 
                    [Default] => 
                    [Extra] => 
                )

            [3] => Array
                (
                    [Field] => enabled
                    [Type] => int(11)
                    [Null] => NO
                    [Key] => 
                    [Default] => 
                    [Extra] => 
                )

            [4] => Array
                (
                    [Field] => email
                    [Type] => varchar(255)
                    [Null] => YES
                    [Key] => 
                    [Default] => 
                    [Extra] => 
                )

            [5] => Array
                (
                    [Field] => password
                    [Type] => varchar(255)
                    [Null] => YES
                    [Key] => 
                    [Default] => 
                    [Extra] => 
                )

            [6] => Array
                (
                    [Field] => username
                    [Type] => varchar(255)
                    [Null] => YES
                    [Key] => 
                    [Default] => 
                    [Extra] => 
                )

            [7] => Array
                (
                    [Field] => name
                    [Type] => varchar(255)
                    [Null] => YES
                    [Key] => 
                    [Default] => 
                    [Extra] => 
                )

            [8] => Array
                (
                    [Field] => surname
                    [Type] => varchar(255)
                    [Null] => YES
                    [Key] => 
                    [Default] => 
                    [Extra] => 
                )

        )

)

答案 1 :(得分:0)

使用你开始时的内容而不是告诉你重新开始,假设你正在维护一个旧的应用程序。

然而,如果这是一个新的应用程序,你真的应该花时间学习mysqli或PDO。

function getform($table)  {
    $html= "";  
    $result = mysql_query("Select * From $table");
    if (!$result) {
       echo 'Could not run query: ' . mysql_error();
       // return from function here. don't know what value you need.
       return "";
    }

    $fieldnames = array();
    for( $i=0; $i < mysql_num_fields($result); $i++ ) {
       $t = array();
       $t['name'] = mysql_field_name( $result , $i );
       $t['type'] = mysql_field_type( $result , $i );
       $t['len'] = mysql_field_len( $result, $i );
       $t['flags'] = mysql_field_flags( $result, $i ); 

       $fieldnames[] = $t;
    }
}

这应该产生类似的东西:

Array
(
    [0] => Array
        (
            [name] => ID
            [type] => int
            [len] => 5
            [flags] => not_null primary_key unique_key multiple_key unsigned auto_increment
        )

    [1] => Array
        (
            [name] => CallID
            [type] => int
            [len] => 7
            [flags] => multiple_key unsigned
        )

    [2] => Array
        (
            [name] => Complaint
            [type] => string
            [len] => 255
            [flags] => multiple_key
        )

    [3] => Array
        (
            [name] => Date
            [type] => date
            [len] => 10
            [flags] => multiple_key binary
        )