9.18.1 Problem
You want to know whether you can create a table using a given table type.
9.18.2 Solution
Ask the server which table types it supports.
9.18.3 Discussion
SHOW VARIABLES can tell you whether or not certain table types are available. For example, you can test the have_bdb, have_innodb, and have_gemini variables to see if the server supports transaction-safe tables. The following PHP code demonstrates how to check for a value of YES for the have_bdb variable, using a two-stage approach. First, make sure that the variable exists by testing for a nonempty result set. (The variable will not be present at all if your version of MySQL predates the inclusion of support for the table type.) Then, fetch the variable's value to see if it's YES:
$avail = FALSE; # escape the variable name properly $var_name = ereg_replace ("([%_])", "\\1", "have_bdb"); if ($result_id = mysql_query ("SHOW VARIABLES LIKE '$var_name'", $conn_id)) { if ($row = mysql_fetch_row ($result_id)) $avail = ($row[1] == "YES" ? TRUE : FALSE); mysql_free_result ($result_id); }
After this code executes, $avail will be TRUE or FALSE to indicate whether the server supports BDB tables. To check for a different table type, modify the code to test the appropriate server variable name.
A more general approach is to write a function that checks for all known handlers and returns a list of the ones that are supported. You cannot ask the server for a list of types directly, but if you know what the possible types are, you can determine which of them are supported using the following rules:
These rules are implemented by the Perl function get_table_handlers( ) shown below. It returns a list containing the words drawn from the list bdb, gemini, innodb, isam, and myisam, corresponding to those table types that are found to be supported:
sub get_table_handlers { my $dbh = shift; my @types; my %typemap = ( "have_bdb" => "bdb", "have_gemini" => "gemini", "have_innodb" => "innodb", "have_innobase" => "innodb", # obsolete form of have_innodb "have_isam" => "isam" ); # get server version number my $ver_num = (get_server_version ($dbh))[1]; # numeric form if ($ver_num < 32300) # only ISAM available prior to 3.23.xx { @types = ("isam"); } else { @types = ("myisam"); # MyISAM always available as of 3.23.xx # Issue SHOW VARIABLES query to get the 'have_' server variables # that indicate presence of table handlers. (There are some 'have_' # variables that don't pertain to table types, but it's still more # efficient to issue a single query than a query for each variable.) my $sth = $dbh->prepare ("SHOW VARIABLES LIKE 'have\_%'"); $sth->execute ( ); while (my ($var, $val) = $sth->fetchrow_array ( )) { push (@types, $typemap{$var}) if exists ($typemap{$var}) && $val eq "YES"; } } return (sort (@types)); }
Using the mysql Client Program
Writing MySQL-Based Programs
Record Selection Techniques
Working with Strings
Working with Dates and Times
Sorting Query Results
Generating Summaries
Modifying Tables with ALTER TABLE
Obtaining and Using Metadata
Importing and Exporting Data
Generating and Using Sequences
Using Multiple Tables
Statistical Techniques
Handling Duplicates
Performing Transactions
Introduction to MySQL on the Web
Incorporating Query Resultsinto Web Pages
Processing Web Input with MySQL
Using MySQL-Based Web Session Management
Appendix A. Obtaining MySQL Software
Appendix B. JSP and Tomcat Primer
Appendix C. References