[PHP] get emum values

2002-02-24 Thread Nick Wilson

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Hi all
Which php function can I use to get the emum values from a mysql db
table? I remember doing this once before but I've been looking at the
manual and just can't remember how I did it?

Many thanks
- -- 
- ---
 www.explodingnet.com   |Projects, Forums and
+Articles for website owners 
- -- Nick Wilson -- |and designers.

-BEGIN PGP SIGNATURE-
Version: GnuPG v1.0.6 (GNU/Linux)

iD8DBQE8eTgCHpvrrTa6L5oRAl0uAKCzvaf13t5bmwLDvjfYpdnbzAn/8wCdHHVC
/N9ePmxrZ4xP3dMx35CQ8Dc=
=EzhU
-END PGP SIGNATURE-

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] get emum values

2002-02-24 Thread Jason Wong

On Monday 25 February 2002 02:59, Nick Wilson wrote:
 Hi all
 Which php function can I use to get the emum values from a mysql db
 table? I remember doing this once before but I've been looking at the
 manual and just can't remember how I did it?

Not sure whether there is a built-in function for doing so. But here's 
something I prepared earlier:

#===
# create_list_from_ENUM
#---
# Returns array [enum vals]
#
# Arguments
# -
#
# $dbh: a $dbh object defined in class.DBI.inc
# $table  : the table to get the ENUM values from
# $column : the column containing the ENUM values
#
#===
function create_list_from_ENUM($dbh, $table, $column) {
  $sth = $dbh-prepare(SHOW COLUMNS FROM $table LIKE '$column');
  if ($sth) {
$sth-execute();
while ($row = $sth-fetchrow_hash()) {
  ereg(('(.*)'), $row[1], $temp);
  $array = explode( ',', $temp[2] );
  while (list ($key, $val) = each ($array)) {
$return[] = $val;
  }
}
  }
  return $return;
}


-- 
Jason Wong - Gremlins Associates - www.gremlins.com.hk

/*
Lies!  All lies!  You're all lying against my boys!
-- Ma Barker
*/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] get emum values

2002-02-24 Thread Matt

I think I got this off the Zend site a year or so ago:
//
function mysql_fetch_enumerations($db_connection, $table_name, $column_name,
$sorted=unsorted) {
/*
Author
Thomas J. Swan
Revision History
1.0Creation of function
1.1Added the ability to sort the array before returning it
Parameters
$db_connection an established mysql connection to the correct
database
$table_namename of Table containing the ENUMeration
$column_namecolumn whose possible values are to be listed
$sortedsort array in ascending,descending, or unsorted
order
Returns
Array of valid values for an enumerated type
Example
$bob = mysql_fetch_enumerations($mysql_connection, Event,
age_group);
while(list($key,$value) = each($bob)) {
echo $value .  ;
}
echo \n;
*/
 $table = addslashes($table);
 $field = addslashes($field);
if ($result = mysql_query(SHOW COLUMNS FROM $table_name LIKE
\$column_name\,$db_connection)) {
$temp = mysql_fetch_array($result);
if (strstr(strtoupper($temp['Type']),ENUM)) {
$str = StripSlashes(substr($temp['Type'],
(strpos($temp['Type'],(,0))+1, (strpos($temp['Type'],\),0)-1)));
$tok = strtok($str,,);
while ($tok) {
$labels[] = substr($tok,(1),(strlen($tok) - 2));
$tok = strtok(,);
}
switch(strtolower($sorted)) {
case ascending :
sort($labels);
break;
case descending :
rsort($labels);
break;
default :
break;
}
return $labels;
} else {
echo (Error not ENUM type\n);
}
} else {
echo (Error querying database.\n);
echo mysql_errno( ).: .mysql_error( ).\n;
}
return false;
}
- Original Message -
From: Nick Wilson [EMAIL PROTECTED]
Subject: [PHP] get emum values


 -BEGIN PGP SIGNED MESSAGE-
 Hash: SHA1

 Hi all
 Which php function can I use to get the emum values from a mysql db
 table?


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] get emum values

2002-02-24 Thread Nick Wilson

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1


* and then Jason Wong declared
 Not sure whether there is a built-in function for doing so. But here's 
 something I prepared earlier:
 
 function create_list_from_ENUM($dbh, $table, $column) {
   $sth = $dbh-prepare(SHOW COLUMNS FROM $table LIKE '$column');
   if ($sth) {
 $sth-execute();
 while ($row = $sth-fetchrow_hash()) {
   ereg(('(.*)'), $row[1], $temp);
   $array = explode( ',', $temp[2] );
   while (list ($key, $val) = each ($array)) {
 $return[] = $val;
   }
 }
   }
   return $return;
 }

Yep, It's come back to me now. Your right of course there is no php
function for this, it's the 'show columns' sql that does the trick.
Cheers!
- -- 
- ---
 www.explodingnet.com   |Projects, Forums and
+Articles for website owners 
- -- Nick Wilson -- |and designers.

-BEGIN PGP SIGNATURE-
Version: GnuPG v1.0.6 (GNU/Linux)

iD8DBQE8eT6kHpvrrTa6L5oRAkQ3AJkB9oYyoidJUlfyUF9LZYe0nUh/rACdGOUj
LkdsHqhrHequJVuhKGi/HLM=
=hbeU
-END PGP SIGNATURE-

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php