note 37399 added to function.mysql-list-fields

From: Date: Thu, 13 Nov 2003 18:40:58 +0000
Subject: note 37399 added to function.mysql-list-fields
Groups: php.notes 
Request: Send a blank email to php-notes+get-60347@lists.php.net to get a copy of this message
If you're willing to use a lot of functions to get a little information about fields in a table, this function is for you. If you just want to get all the information you can find, you can use this: <?php // Taken from: http://ca.php.net/manual/en/function.mysql-field-flags.php // Original by: amir at scrounch dot com // Returns the name of the fields in the primary key for a table. // Also keeps the fields in KEY order. Handy. function getPrimaryKeyOf($table) { $keys = Array(); $query = sprintf("SHOW KEYS FROM %s", $table); $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { if ( $row['Key_name'] == 'PRIMARY' ) $keys[$row['Seq_in_index'] - 1] = $row['Column_name']; } return $keys; } // Returns a bunch of information about a table... // The name of the auto-increment field, if any, fields in the // primary key (using the function above), and all information // about all fields. function getTableInformationOf($table) { $information = array( "auto" => "", "primary" => array(), "fields" => array() ); $information['primary'] = $this->getPrimaryKeyOf($table); $result = mysql_query("DESC $table"); while ( $field = mysql_fetch_assoc($result) ) { $information['fields'][] = $field; if ( $field['Extra'] == "auto_increment" ) $information['auto'] = $field['Field']; } return $information; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.mysql-list-fields.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+37399 Delete -- http://master.php.net/manage/user-notes.php?action=delete+37399&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+37399&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#60347) next »