note 73755 added to function.mysql-query

From: Date: Fri, 09 Mar 2007 00:01:17 +0000
Subject: note 73755 added to function.mysql-query
Groups: php.notes 
Request: Send a blank email to php-notes+get-124253@lists.php.net to get a copy of this message
If you're looking to create a dynamic dropdown list or pull the possible values of an ENUM field for other reasons, here's a handy function: <?php // Function to Return All Possible ENUM Values for a Field function getEnumValues($table, $field) { $enum_array = array(); $query = 'SHOW COLUMNS FROM ' . $table . ' LIKE "' . $field . '"'; $result = mysql_query($query); $row = mysql_fetch_row($result); preg_match_all('/\'(.*?)\'/', $row[1], $enum_array); if(!empty($enum_array[1])) { // Shift array keys to match original enumerated index in MySQL (allows for use of index values instead of strings) foreach($enum_array[1] as $mkey => $mval) $enum_fields[$mkey+1] = $mval; return $enum_fields; } else return array(); // Return an empty array to avoid possible errors/warnings if array is passed to foreach() without first being checked with !empty(). } ?> This function asumes an existing MySQL connection and that desired DB is already selected. Since this function returns an array with the original enumerated index numbers, you can use these in any later UPDATEs or INSERTS in your script instead of having to deal with the string values. Also, since these are integers, you can typecast them as such using (int) when building your queries--which is much easer for SQL injection filtering than a string value. ---- Server IP: 64.71.164.2 Probable Submitter: 72.34.75.194 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-query.php Edit -- https://master.php.net/note/edit/73755 Del: integrated -- https://master.php.net/note/delete/73755/integrated Del: useless -- https://master.php.net/note/delete/73755/useless Del: bad code -- https://master.php.net/note/delete/73755/bad+code Del: spam -- https://master.php.net/note/delete/73755/spam Del: non-english -- https://master.php.net/note/delete/73755/non-english Del: in docs -- https://master.php.net/note/delete/73755/in+docs Del: other reasons-- https://master.php.net/note/delete/73755 Reject -- https://master.php.net/note/reject/73755 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124253) next »