Database Reference
In-Depth Information
# basic DSN
dsn = "DBI:Mysql:database=cookbook"
# look in standard option files; use [cookbook] and [client] groups
dsn << ";mysql_read_default_group=cookbook"
dbh = DBI . connect ( dsn , nil , nil )
The following example uses the .my.cnf file in the current user's home directory to obtain
parameters from the [client] group:
# basic DSN
dsn = "DBI:Mysql:database=cookbook"
# look in user-specific option file owned by the current user
dsn << ";mysql_read_default_file= #{ ENV [ 'HOME' ] } /.my.cnf"
dbh = DBI . connect ( dsn , nil , nil )
PHP. As mentioned earlier, the PDO MySQL driver does not necessarily support using
MySQL option files (it does not if you use mysqlnd as the underlying library). To work
around that limitation, use a function that reads an option file, such as the
read_mysql_option_file() function shown in the following listing. It takes as argu‐
ments the name of an option file and an option group name or an array containing
group names. (Group names should be written without square brackets.) It then reads
any options present in the file for the named group or groups. If no option group ar‐
gument is given, the function looks by default in the [client] group. The return value
is an array of option name/value pairs, or FALSE if an error occurs. It is not an error for
the file not to exist. (Note that quoted option values and trailing # -style comments
following option values are legal in MySQL option files, but this function does not
handle those constructs.)
function read_mysql_option_file ( $filename , $group_list = "client" )
{
if ( is_string ( $group_list )) # convert string to array
$group_list = array ( $group_list );
if ( ! is_array ( $group_list )) # hmm ... garbage argument?
return ( FALSE );
$opt = array (); # option name/value array
if ( !@ ( $fp = fopen ( $filename , "r" ))) # if file does not exist,
return ( $opt ); # return an empty list
$in_named_group = 0 ; # set nonzero while processing a named group
while ( $s = fgets ( $fp , 1024 ))
{
$s = trim ( $s );
if ( preg_match ( "/^[#;]/" , $s )) # skip comments
continue ;
if ( preg_match ( "/^\[([^]]+)]/" , $s , $arg )) # option group line?
{
# check whether we are in one of the desired groups
$in_named_group = 0 ;
foreach ( $group_list as $group_name )
{
if ( $arg [ 1 ] == $group_name )
Search WWH ::




Custom Search