home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / mysql.pod < prev    next >
Encoding:
Text File  |  2003-02-07  |  33.6 KB  |  1,272 lines

  1. =pod
  2.  
  3. =head1 NAME
  4.  
  5. DBD::mysql - MySQL driver for the Perl5 Database Interface (DBI)
  6.  
  7. =head1 SYNOPSIS
  8.  
  9.     use DBI;
  10.  
  11.     $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port";
  12.  
  13.     $dbh = DBI->connect($dsn, $user, $password);
  14.  
  15.  
  16.     $drh = DBI->install_driver("mysql");
  17.     @databases = DBI->data_sources("mysql");
  18.        or
  19.     @databases = DBI->data_sources("mysql",
  20.                    {"host" => $host, "port" => $port});
  21.  
  22.     $sth = $dbh->prepare("SELECT * FROM foo WHERE bla");
  23.        or
  24.     $sth = $dbh->prepare("LISTFIELDS $table");
  25.        or
  26.     $sth = $dbh->prepare("LISTINDEX $table $index");
  27.     $sth->execute;
  28.     $numRows = $sth->rows;
  29.     $numFields = $sth->{'NUM_OF_FIELDS'};
  30.     $sth->finish;
  31.  
  32.     $rc = $drh->func('createdb', $database, $host, $user, $password, 'admin');
  33.     $rc = $drh->func('dropdb', $database, $host, $user, $password, 'admin');
  34.     $rc = $drh->func('shutdown', $host, $user, $password, 'admin');
  35.     $rc = $drh->func('reload', $host, $user, $password, 'admin');
  36.  
  37.     $rc = $dbh->func('createdb', $database, 'admin');
  38.     $rc = $dbh->func('dropdb', $database, 'admin');
  39.     $rc = $dbh->func('shutdown', 'admin');
  40.     $rc = $dbh->func('reload', 'admin');
  41.  
  42.  
  43. =head1 EXAMPLE
  44.  
  45.   #!/usr/bin/perl
  46.  
  47.   use strict;
  48.   use DBI();
  49.  
  50.   # Connect to the database.
  51.   my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost",
  52.                          "joe", "joe's password",
  53.                          {'RaiseError' => 1});
  54.  
  55.   # Drop table 'foo'. This may fail, if 'foo' doesn't exist.
  56.   # Thus we put an eval around it.
  57.   eval { $dbh->do("DROP TABLE foo") };
  58.   print "Dropping foo failed: $@\n" if $@;
  59.  
  60.   # Create a new table 'foo'. This must not fail, thus we don't
  61.   # catch errors.
  62.   $dbh->do("CREATE TABLE foo (id INTEGER, name VARCHAR(20))");
  63.  
  64.   # INSERT some data into 'foo'. We are using $dbh->quote() for
  65.   # quoting the name.
  66.   $dbh->do("INSERT INTO foo VALUES (1, " . $dbh->quote("Tim") . ")");
  67.  
  68.   # Same thing, but using placeholders
  69.   $dbh->do("INSERT INTO foo VALUES (?, ?)", undef, 2, "Jochen");
  70.  
  71.   # Now retrieve data from the table.
  72.   my $sth = $dbh->prepare("SELECT * FROM foo");
  73.   $sth->execute();
  74.   while (my $ref = $sth->fetchrow_hashref()) {
  75.     print "Found a row: id = $ref->{'id'}, name = $ref->{'name'}\n";
  76.   }
  77.   $sth->finish();
  78.  
  79.   # Disconnect from the database.
  80.   $dbh->disconnect();
  81.  
  82.  
  83. =head1 DESCRIPTION
  84.  
  85. B<DBD::mysql> is the Perl5 Database Interface driver for the MySQL
  86. database. In other words: DBD::mysql is an interface between the Perl
  87. programming language and the MySQL programming API that comes with
  88. the MySQL relational database management system. Most functions
  89. provided by this programming API are supported. Some rarely used
  90. functions are missing, mainly because noone ever requested
  91. them. :-)
  92.  
  93. In what follows we first discuss the use of DBD::mysql,
  94. because this is what you will need the most. For installation, see the
  95. sections on L<INSTALLATION>, L<WIN32 INSTALLATION>, and L<KNOWN BUGS>
  96. below. See L<EXAMPLE> for a simple example above.
  97.  
  98. From perl you activate the interface with the statement
  99.  
  100.     use DBI;
  101.  
  102. After that you can connect to multiple MySQL database servers
  103. and send multiple queries to any of them via a simple object oriented
  104. interface. Two types of objects are available: database handles and
  105. statement handles. Perl returns a database handle to the connect
  106. method like so:
  107.  
  108.   $dbh = DBI->connect("DBI:mysql:database=$db;host=$host",
  109.               $user, $password, {RaiseError => 1});
  110.  
  111. Once you have connected to a database, you can can execute SQL
  112. statements with:
  113.  
  114.   my $query = sprintf("INSERT INTO foo VALUES (%d, %s)",
  115.               $number, $dbh->quote("name"));
  116.   $dbh->do($query);
  117.  
  118. See L<DBI(3)> for details on the quote and do methods. An alternative
  119. approach is
  120.  
  121.   $dbh->do("INSERT INTO foo VALUES (?, ?)", undef,
  122.        $number, $name);
  123.  
  124. in which case the quote method is executed automatically. See also
  125. the bind_param method in L<DBI(3)>. See L<DATABASE HANDLES> below
  126. for more details on database handles.
  127.  
  128. If you want to retrieve results, you need to create a so-called
  129. statement handle with:
  130.  
  131.   $sth = $dbh->prepare("SELECT * FROM $table");
  132.   $sth->execute();
  133.  
  134. This statement handle can be used for multiple things. First of all
  135. you can retreive a row of data:
  136.  
  137.   my $row = $sth->fetchow_hashref();
  138.  
  139. If your table has columns ID and NAME, then $row will be hash ref with
  140. keys ID and NAME. See L<STATEMENT HANDLES> below for more details on
  141. statement handles.
  142.  
  143. But now for a more formal approach:
  144.  
  145.  
  146. =head2 Class Methods
  147.  
  148. =over
  149.  
  150. =item B<connect>
  151.  
  152.     use DBI;
  153.  
  154.     $dsn = "DBI:mysql:$database";
  155.     $dsn = "DBI:mysql:database=$database;host=$hostname";
  156.     $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port";
  157.  
  158.     $dbh = DBI->connect($dsn, $user, $password);
  159.  
  160. A C<database> must always be specified.
  161.  
  162. =over
  163.  
  164. =item host
  165.  
  166. =item port
  167.  
  168. The hostname, if not specified or specified as '', will default to an
  169. MySQL daemon running on the local machine on the default port
  170. for the UNIX socket.
  171.  
  172. Should the MySQL daemon be running on a non-standard port number,
  173. you may explicitly state the port number to connect to in the C<hostname>
  174. argument, by concatenating the I<hostname> and I<port number> together
  175. separated by a colon ( C<:> ) character or by using the  C<port> argument.
  176.  
  177.  
  178. =item mysql_client_found_rows
  179.  
  180. Enables (TRUE value) or disables (FALSE value) the flag CLIENT_FOUND_ROWS
  181. while connecting to the MySQL server. This has a somewhat funny effect:
  182. Without mysql_client_found_rows, if you perform a query like
  183.  
  184.   UPDATE $table SET id = 1 WHERE id = 1
  185.  
  186. then the MySQL engine will always return 0, because no rows have changed.
  187. With mysql_client_found_rows however, it will return the number of rows
  188. that have an id 1, as some people are expecting. (At least for compatibility
  189. to other engines.)
  190.  
  191. =item mysql_compression
  192.  
  193. As of MySQL 3.22.3, a new feature is supported: If your DSN contains
  194. the option "mysql_compression=1", then the communication between client
  195. and server will be compressed.
  196.  
  197. =item mysql_connect_timeout
  198.  
  199. If your DSN contains the option "mysql_connect_timeout=##", the connect
  200. request to the server will timeout if it has not been successful after
  201. the given number of seconds.
  202.  
  203. =item mysql_read_default_file
  204.  
  205. =item mysql_read_default_group
  206.  
  207. These options can be used to read a config file like /etc/my.cnf or
  208. ~/.my.cnf. By default MySQL's C client library doesn't use any config
  209. files unlike the client programs (mysql, mysqladmin, ...) that do, but
  210. outside of the C client library. Thus you need to explicitly request
  211. reading a config file, as in
  212.  
  213.     $dsn = "DBI:mysql:test;mysql_read_default_file=/home/joe/my.cnf";
  214.     $dbh = DBI->connect($dsn, $user, $password)
  215.  
  216. The option mysql_read_default_group can be used to specify the default
  217. group in the config file: Usually this is the I<client> group, but
  218. see the following example:
  219.  
  220.     [client]
  221.     host=localhost
  222.  
  223.     [perl]
  224.     host=perlhost
  225.  
  226. (Note the order of the entries! The example won't work, if you reverse
  227. the [client] and [perl] sections!)
  228.  
  229. If you read this config file, then you'll be typically connected to
  230. I<localhost>. However, by using
  231.  
  232.     $dsn = "DBI:mysql:test;mysql_read_default_group=perl;"
  233.         . "mysql_read_default_file=/home/joe/my.cnf";
  234.     $dbh = DBI->connect($dsn, $user, $password);
  235.  
  236. you'll be connected to I<perlhost>. Note that if you specify a
  237. default group and do not specify a file, then the default config
  238. files will all be read.  See the documentation of
  239. the C function mysql_options() for details.
  240.  
  241. =item mysql_socket
  242.  
  243. As of MySQL 3.21.15, it is possible to choose the Unix socket that is
  244. used for connecting to the server. This is done, for example, with
  245.  
  246.     mysql_socket=/dev/mysql
  247.  
  248. Usually there's no need for this option, unless you are using another
  249. location for the socket than that built into the client.
  250.  
  251. =item mysql_ssl
  252.  
  253. A true value turns on the CLIENT_SSL flag when connecting to the MySQL
  254. database:
  255.  
  256.   mysql_ssl=1
  257.  
  258. This means that your communication with the server will be encrypted.
  259.  
  260. If you turn mysql_ssl on, you might also wish to use the following
  261. flags:
  262.  
  263. =item mysql_ssl_client_key
  264.  
  265. =item mysql_ssl_client_cert
  266.  
  267. =item mysql_ssl_ca_file
  268.  
  269. =item mysql_ssl_ca_path
  270.  
  271. =item mysql_ssl_cipher
  272.  
  273. These are used to specify the respective parameters of a call
  274. to mysql_ssl_set, if mysql_ssl is turned on.  
  275.  
  276.  
  277. =item mysql_local_infile
  278.  
  279. As of MySQL 3.23.49, the LOCAL capability for LOAD DATA may be disabled
  280. in the MySQL client library by default. If your DSN contains the option
  281. "mysql_local_infile=1", LOAD DATA LOCAL will be enabled.  (However,
  282. this option is effective if the server has also been configured to
  283. disallow LOCAL.)
  284.  
  285.  
  286. =back
  287.  
  288. =back
  289.  
  290.  
  291. =head2 Private MetaData Methods
  292.  
  293. =over
  294.  
  295. =item B<ListDBs>
  296.  
  297.     my $drh = DBI->install_driver("mysql");
  298.     @dbs = $drh->func("$hostname:$port", '_ListDBs');
  299.     @dbs = $drh->func($hostname, $port, '_ListDBs');
  300.     @dbs = $dbh->func('_ListDBs');
  301.  
  302. Returns a list of all databases managed by the MySQL daemon
  303. running on C<$hostname>, port C<$port>. This method
  304. is rarely needed for databases running on C<localhost>: You should
  305. use the portable method
  306.  
  307.     @dbs = DBI->data_sources("mysql");
  308.  
  309. whenever possible. It is a design problem of this method, that there's
  310. no way of supplying a host name or port number to C<data_sources>, that's
  311. the only reason why we still support C<ListDBs>. :-(
  312.  
  313. =back
  314.  
  315.  
  316. =head2 Server Administration
  317.  
  318. =over
  319.  
  320. =item admin
  321.  
  322.     $rc = $drh->func("createdb", $dbname, [host, user, password,], 'admin');
  323.     $rc = $drh->func("dropdb", $dbname, [host, user, password,], 'admin');
  324.     $rc = $drh->func("shutdown", [host, user, password,], 'admin');
  325.     $rc = $drh->func("reload", [host, user, password,], 'admin');
  326.  
  327.       or
  328.  
  329.     $rc = $dbh->func("createdb", $dbname, 'admin');
  330.     $rc = $dbh->func("dropdb", $dbname, 'admin');
  331.     $rc = $dbh->func("shutdown", 'admin');
  332.     $rc = $dbh->func("reload", 'admin');
  333.  
  334. For server administration you need a server connection. For obtaining
  335. this connection you have two options: Either use a driver handle (drh)
  336. and supply the appropriate arguments (host, defaults localhost, user,
  337. defaults to '' and password, defaults to ''). A driver handle can be
  338. obtained with
  339.  
  340.     $drh = DBI->install_driver('mysql');
  341.  
  342. Otherwise reuse the existing connection of a database handle (dbh).
  343.  
  344. There's only one function available for administrative purposes, comparable
  345. to the m(y)sqladmin programs. The command being execute depends on the
  346. first argument:
  347.  
  348. =over
  349.  
  350. =item createdb
  351.  
  352. Creates the database $dbname. Equivalent to "m(y)sqladmin create $dbname".
  353.  
  354. =item dropdb
  355.  
  356. Drops the database $dbname. Equivalent to "m(y)sqladmin drop $dbname".
  357.  
  358. It should be noted that database deletion is
  359. I<not prompted for> in any way.  Nor is it undo-able from DBI.
  360.  
  361.     Once you issue the dropDB() method, the database will be gone!
  362.  
  363. These method should be used at your own risk.
  364.  
  365. =item shutdown
  366.  
  367. Silently shuts down the database engine. (Without prompting!)
  368. Equivalent to "m(y)sqladmin shutdown".
  369.  
  370. =item reload
  371.  
  372. Reloads the servers configuration files and/or tables. This can be particularly
  373. important if you modify access privileges or create new users.
  374.  
  375. =back
  376.  
  377. =back
  378.  
  379.  
  380. =head1 DATABASE HANDLES
  381.  
  382. The DBD::mysql driver supports the following attributes of database
  383. handles (read only):
  384.  
  385.   $errno = $dbh->{'mysql_errno'};
  386.   $error = $dbh->{'mysql_error};
  387.   $info = $dbh->{'mysql_hostinfo'};
  388.   $info = $dbh->{'mysql_info'};
  389.   $insertid = $dbh->{'mysql_insertid'};
  390.   $info = $dbh->{'mysql_protoinfo'};
  391.   $info = $dbh->{'mysql_serverinfo'};
  392.   $info = $dbh->{'mysql_stat'};
  393.   $threadId = $dbh->{'mysql_thread_id'};
  394.  
  395. These correspond to mysql_errno(), mysql_error(), mysql_get_host_info(),
  396. mysql_info(), mysql_insert_id(), mysql_get_proto_info(),
  397. mysql_get_server_info(), mysql_stat() and mysql_thread_id(),
  398. respectively.
  399.  
  400.  
  401. =head1 STATEMENT HANDLES
  402.  
  403. The statement handles of DBD::mysql support a number
  404. of attributes. You access these by using, for example,
  405.  
  406.   my $numFields = $sth->{'NUM_OF_FIELDS'};
  407.  
  408. Note, that most attributes are valid only after a successfull I<execute>.
  409. An C<undef> value will returned in that case. The most important exception
  410. is the C<mysql_use_result> attribute: This forces the driver to use
  411. mysql_use_result rather than mysql_store_result. The former is faster
  412. and less memory consuming, but tends to block other processes. (That's why
  413. mysql_store_result is the default.)
  414.  
  415. To set the C<mysql_use_result> attribute, use either of the following:
  416.  
  417.   my $sth = $dbh->prepare("QUERY", { "mysql_use_result" => 1});
  418.  
  419. or
  420.  
  421.   my $sth = $dbh->prepare("QUERY");
  422.   $sth->{"mysql_use_result"} = 1;
  423.  
  424. Column dependent attributes, for example I<NAME>, the column names,
  425. are returned as a reference to an array. The array indices are
  426. corresponding to the indices of the arrays returned by I<fetchrow>
  427. and similar methods. For example the following code will print a
  428. header of table names together with all rows:
  429.  
  430.   my $sth = $dbh->prepare("SELECT * FROM $table");
  431.   if (!$sth) {
  432.       die "Error:" . $dbh->errstr . "\n";
  433.   }
  434.   if (!$sth->execute) {
  435.       die "Error:" . $sth->errstr . "\n";
  436.   }
  437.   my $names = $sth->{'NAME'};
  438.   my $numFields = $sth->{'NUM_OF_FIELDS'};
  439.   for (my $i = 0;  $i < $numFields;  $i++) {
  440.       printf("%s%s", $i ? "," : "", $$names[$i]);
  441.   }
  442.   print "\n";
  443.   while (my $ref = $sth->fetchrow_arrayref) {
  444.       for (my $i = 0;  $i < $numFields;  $i++) {
  445.       printf("%s%s", $i ? "," : "", $$ref[$i]);
  446.       }
  447.       print "\n";
  448.   }
  449.  
  450. For portable applications you should restrict yourself to attributes with
  451. capitalized or mixed case names. Lower case attribute names are private
  452. to DBD::mysql. The attribute list includes:
  453.  
  454. =over
  455.  
  456. =item ChopBlanks
  457.  
  458. this attribute determines whether a I<fetchrow> will chop preceding
  459. and trailing blanks off the column values. Chopping blanks does not
  460. have impact on the I<max_length> attribute.
  461.  
  462. =item mysql_insertid
  463.  
  464. MySQL has the ability to choose unique key values automatically. If this
  465. happened, the new ID will be stored in this attribute. An alternative
  466. way for accessing this attribute is via $dbh->{'mysql_insertid'}.
  467. (Note we are using the $dbh in this case!)
  468.  
  469. =item mysql_is_blob
  470.  
  471. Reference to an array of boolean values; TRUE indicates, that the
  472. respective column is a blob. This attribute is valid for MySQL only.
  473.  
  474. =item mysql_is_key
  475.  
  476. Reference to an array of boolean values; TRUE indicates, that the
  477. respective column is a key. This is valid for MySQL only.
  478.  
  479. =item mysql_is_num
  480.  
  481. Reference to an array of boolean values; TRUE indicates, that the
  482. respective column contains numeric values.
  483.  
  484. =item mysql_is_pri_key
  485.  
  486. Reference to an array of boolean values; TRUE indicates, that the
  487. respective column is a primary key.
  488.  
  489. =item mysql_is_auto_increment
  490.  
  491. Reference to an array of boolean values; TRUE indicates that the
  492. respective column is an AUTO_INCREMENT column.  This is only valid
  493. for MySQL.
  494.  
  495. =item mysql_length
  496.  
  497. =item mysql_max_length
  498.  
  499. A reference to an array of maximum column sizes. The I<max_length> is
  500. the maximum physically present in the result table, I<length> gives
  501. the theoretically possible maximum. I<max_length> is valid for MySQL
  502. only.
  503.  
  504. =item NAME
  505.  
  506. A reference to an array of column names.
  507.  
  508. =item NULLABLE
  509.  
  510. A reference to an array of boolean values; TRUE indicates that this column
  511. may contain NULL's.
  512.  
  513. =item NUM_OF_FIELDS
  514.  
  515. Number of fields returned by a I<SELECT> or I<LISTFIELDS> statement.
  516. You may use this for checking whether a statement returned a result:
  517. A zero value indicates a non-SELECT statement like I<INSERT>,
  518. I<DELETE> or I<UPDATE>.
  519.  
  520. =item mysql_table
  521.  
  522. A reference to an array of table names, useful in a I<JOIN> result.
  523.  
  524. =item TYPE
  525.  
  526. A reference to an array of column types. The engine's native column
  527. types are mapped to portable types like DBI::SQL_INTEGER() or
  528. DBI::SQL_VARCHAR(), as good as possible. Not all native types have
  529. a meaningfull equivalent, for example DBD::mysql::FIELD_TYPE_INTERVAL
  530. is mapped to DBI::SQL_VARCHAR().
  531. If you need the native column types, use I<mysql_type>. See below.
  532.  
  533. =item mysql_type
  534.  
  535. A reference to an array of MySQL's native column types, for example
  536. DBD::mysql::FIELD_TYPE_SHORT() or DBD::mysql::FIELD_TYPE_STRING().
  537. Use the I<TYPE> attribute, if you want portable types like
  538. DBI::SQL_SMALLINT() or DBI::SQL_VARCHAR().
  539.  
  540. =item mysql_type_name
  541.  
  542. Similar to mysql, but type names and not numbers are returned.
  543. Whenever possible, the ANSI SQL name is preferred.
  544.  
  545. =back
  546.  
  547.  
  548. =head1 TRANSACTION SUPPORT
  549.  
  550. Beginning with DBD::mysql 2.0416, transactions are supported.
  551. The transaction support works as follows:
  552.  
  553. =over
  554.  
  555. =item *
  556.  
  557. By default AutoCommit mode is on, following the DBI specifications.
  558.  
  559. =item *
  560.  
  561. If you execute
  562.  
  563.     $dbh-E<gt>{'AutoCommit'} = 0;
  564.  
  565. or
  566.  
  567.     $dbh-E<gt>{'AutoCommit'} = 1;
  568.  
  569. then the driver will set the MySQL server variable autocommit to 0 or
  570. 1, respectively. Switching from 0 to 1 will also issue a COMMIT,
  571. following the DBI specifications.
  572.  
  573. =item *
  574.  
  575. The methods
  576.  
  577.     $dbh-E<gt>rollback();
  578.     $dbh-E<gt>commit();
  579.  
  580. will issue the commands COMMIT and ROLLBACK, respectively. A
  581. ROLLBACK will also be issued if AutoCommit mode is off and the
  582. database handles DESTROY method is called. Again, this is following
  583. the DBI specifications.
  584.  
  585. =back
  586.  
  587. Given the above, you should note the following:
  588.  
  589. =over
  590.  
  591. =item *
  592.  
  593. You should never change the server variable autocommit manually,
  594. unless you are ignoring DBI's transaction support.
  595.  
  596. =item *
  597.  
  598. Switching AutoCommit mode from on to off or vice versa may fail.
  599. You should always check for errors, when changing AutoCommit mode.
  600. The suggested way of doing so is using the DBI flag RaiseError.
  601. If you don't like RaiseError, you have to use code like the
  602. following:
  603.  
  604.   $dbh->{'AutoCommit'} = 0;
  605.   if ($dbh->{'AutoCommit'}) {
  606.     # An error occurred!
  607.   }
  608.  
  609. =item *
  610.  
  611. If you detect an error while changing the AutoCommit mode, you
  612. should no longer use the database handle. In other words, you
  613. should disconnect and reconnect again, because the transaction
  614. mode is unpredictable. Alternatively you may verify the transaction
  615. mode by checking the value of the server variable autocommit.
  616. However, such behaviour isn't portable.
  617.  
  618. =item *
  619.  
  620. DBD::mysql has a "reconnect" feature that handles the so-called
  621. MySQL "morning bug": If the server has disconnected, most probably
  622. due to a timeout, then by default the driver will reconnect and
  623. attempt to execute the same SQL statement again. However, this
  624. behaviour is disabled when AutoCommit is off: Otherwise the
  625. transaction state would be completely unpredictable after a
  626. reconnect.  
  627.  
  628. =back
  629.  
  630.  
  631. =head1 SQL EXTENSIONS
  632.  
  633. Certain metadata functions of MySQL that are available on the
  634. C API level, haven't been implemented here. Instead they are implemented
  635. as "SQL extensions" because they return in fact nothing else but the
  636. equivalent of a statement handle. These are:
  637.  
  638. =over
  639.  
  640. =item LISTFIELDS $table
  641.  
  642. Returns a statement handle that describes the columns of $table.
  643. Ses the docs of mysql_list_fields (C API) for details.
  644.  
  645. =back
  646.  
  647.  
  648.  
  649. =head1 COMPATIBILITY ALERT
  650.  
  651. The statement attribute I<TYPE> has changed its meaning, as of
  652. DBD::mysql 2.0119. Formerly it used to be the an array
  653. of native engine's column types, but it is now an array of
  654. portable SQL column types. The old attribute is still available
  655. as I<mysql_type>.
  656.  
  657. DBD::mysql is a moving target, due to a number of reasons:
  658.  
  659. =over
  660.  
  661. =item -
  662.  
  663. Of course we have to conform the DBI guidelines and developments.
  664.  
  665. =item -
  666.  
  667. We have to keep track with the latest MySQL developments.
  668.  
  669. =item -
  670.  
  671. And, surprisingly, we have to be as close to ODBC as possible: This is
  672. due to the current direction of DBI.
  673.  
  674. =item -
  675.  
  676. And, last not least, as any tool it has a little bit life of its own.
  677.  
  678. =back
  679.  
  680. This means that a lot of things had to and have to be changed.
  681. As I am not interested in maintaining a lot of compatibility kludges,
  682. which only increase the drivers code without being really usefull,
  683. I did and will remove some features, methods or attributes.
  684.  
  685. To ensure a smooth upgrade, the following policy will be applied:
  686.  
  687. =over
  688.  
  689. =item Obsolete features
  690.  
  691. The first step is to declare something obsolete. This means, that no code
  692. is changed, but the feature appears in the list of obsolete features. See
  693. L<Obsolete Features> below.
  694.  
  695. =item Deprecated features
  696.  
  697. If the feature has been obsolete for quite some time, typically in the
  698. next major stable release, warnings will be inserted in the code. You
  699. can suppress these warnings by setting
  700.  
  701.     $DBD::mysql = 1;
  702.  
  703. In the docs the feature will be moved from the list of obsolete features
  704. to the list of deprecated features. See L<Deprecated Features> below.
  705.  
  706. =item Removing features
  707.  
  708. Finally features will be removed silently in the next major stable
  709. release. The feature will be shown in the list of historic features.
  710. See L<Historic Features> below.
  711.  
  712. =back
  713.  
  714. Example: The statement handle attribute
  715.  
  716.     $sth->{'LENGTH'}
  717.  
  718. was declared obsolete in DBD::mysql 2.00xy. It was considered
  719. deprecated in DBD::mysql 2.02xy and removed in 2.04xy.
  720.  
  721.  
  722. =head2 Obsolete Features
  723.  
  724. =over
  725.  
  726. =item Database handle attributes
  727.  
  728. The following database handle attributes are declared obsolete
  729. in DBD::mysql 2.09. They will be deprecated in 2.11 and removed
  730. in 2.13.
  731.  
  732. =over
  733.  
  734. =item C<$dbh-E<gt>{'errno'}>
  735.  
  736. Replaced by C<$dbh-E<gt>{'mysql_errno'}>
  737.  
  738. =item C<$dbh-E<gt>{'errmsg'}>
  739.  
  740. Replaced by C<$dbh-E<gt>{'mysql_error'}>
  741.  
  742. =item C<$dbh-E<gt>{'hostinfo'}>
  743.  
  744. Replaced by C<$dbh-E<gt>{'mysql_hostinfo'}>
  745.  
  746. =item C<$dbh-E<gt>{'info'}>
  747.  
  748. Replaced by C<$dbh-E<gt>{'mysql_info'}>
  749.  
  750. =item C<$dbh-E<gt>{'protoinfo'}>
  751.  
  752. Replaced by C<$dbh-E<gt>{'mysql_protoinfo'}>
  753.  
  754. =item C<$dbh-E<gt>{'serverinfo'}>
  755.  
  756. Replaced by C<$dbh-E<gt>{'mysql_serverinfo'}>
  757.  
  758. =item C<$dbh-E<gt>{'stats'}>
  759.  
  760. Replaced by C<$dbh-E<gt>{'mysql_stat'}>
  761.  
  762. =item C<$dbh-E<gt>{'thread_id'}>
  763.  
  764. Replaced by C<$dbh-E<gt>{'mysql_thread_id'}>
  765.  
  766. =back
  767.  
  768. =back
  769.  
  770.  
  771. =head2 Deprecated Features
  772.  
  773. =over
  774.  
  775. =item _ListTables
  776.  
  777. Replace with the standard DBI method C<$dbh-E<gt>tables()>. See also
  778. C<$dbh-E<gt>table_info()>. Portable applications will prefer
  779.  
  780.     @tables = map { $_ =~ s/.*\.//; $_ } $dbh-E<gt>tables()
  781.  
  782. because, depending on the engine, the string "user.table" will be
  783. returned, user being the table owner. The method will be removed
  784. in DBD::mysql version 2.11xy.
  785.  
  786. =back
  787.  
  788.  
  789. =head2 Historic Features
  790.  
  791. =over
  792.  
  793. =item _CreateDB
  794.  
  795. =item _DropDB
  796.  
  797. The methods
  798.  
  799.     $dbh-E<gt>func($db, '_CreateDB');
  800.     $dbh-E<gt>func($db, '_DropDB');
  801.  
  802. have been used for creating or dropping databases. They have been removed
  803. in 1.21_07 in favour of
  804.  
  805.     $drh-E<gt>func("createdb", $dbname, $host, "admin")
  806.     $drh-E<gt>func("dropdb", $dbname, $host, "admin")
  807.  
  808. =item _ListFields
  809.  
  810. The method
  811.  
  812.     $sth = $dbh-E<gt>func($table, '_ListFields');
  813.  
  814. has been used to list a tables columns names, types and other attributes.
  815. This method has been removed in 1.21_07 in favour of
  816.  
  817.     $sth = $dbh-E<gt>prepare("LISTFIELDS $table");
  818.  
  819. =item _ListSelectedFields
  820.  
  821. The method
  822.  
  823.     $sth->func('_ListSelectedFields');
  824.  
  825. use to return a hash ref of attributes like 'IS_NUM', 'IS_KEY' and so
  826. on. These attributes are now accessible via
  827.  
  828.     $sth-E<gt>{'mysql_is_num'};
  829.     $sth-E<gt>{'mysql_is_key'};
  830.  
  831. and so on. Thus the method has been removed in 1.21_07.
  832.  
  833. =item _NumRows
  834.  
  835. The method
  836.  
  837.     $sth-E<gt>func('_NumRows');
  838.  
  839. used to be equivalent to
  840.  
  841.     $sth-E<gt>rows();
  842.  
  843. and has been removed in 1.21_07.
  844.  
  845. =item _InsertID
  846.  
  847. The method
  848.  
  849.     $dbh-E<gt>func('_InsertID');
  850.  
  851. used to be equivalent with
  852.  
  853.     $dbh-E<gt>{'mysql_insertid'};
  854.  
  855. =item Statement handle attributes
  856.  
  857. =over
  858.  
  859. =item affected_rows
  860.  
  861. Replaced with $sth-E<gt>{'mysql_affected_rows'} or the result
  862. of $sth-E<gt>execute().
  863.  
  864. =item format_default_size
  865.  
  866. Replaced with $sth-E<gt>{'PRECISION'}.
  867.  
  868. =item format_max_size
  869.  
  870. Replaced with $sth-E<gt>{'mysql_max_length'}.
  871.  
  872. =item format_type_name
  873.  
  874. Replaced with $sth-E<gt>{'TYPE'} (portable) or
  875. $sth-E<gt>{'mysql_type_name'} (MySQL specific).
  876.  
  877. =item format_right_justify
  878.  
  879. Replaced with $sth-E<gt>->{'TYPE'} (portable) or
  880. $sth-E<gt>{'mysql_is_num'} (MySQL specific).
  881.  
  882. =item insertid
  883.  
  884. Replaced with $sth-E<gt>{'mysql_insertid'}.
  885.  
  886. =item IS_BLOB
  887.  
  888. Replaced with $sth-E<gt>{'TYPE'} (portable) or
  889. $sth-E<gt>{'mysql_is_blob'} (MySQL specific).
  890.  
  891. =item is_blob
  892.  
  893. Replaced with $sth-E<gt>{'TYPE'} (portable) or
  894. $sth-E<gt>{'mysql_is_blob'} (MySQL specific).
  895.  
  896. =item IS_PRI_KEY
  897.  
  898. Replaced with $sth-E<gt>{'mysql_is_pri_key'}.
  899.  
  900. =item is_pri_key
  901.  
  902. Replaced with $sth-E<gt>{'mysql_is_pri_key'}.
  903.  
  904. =item IS_NOT_NULL
  905.  
  906. Replaced with $sth-E<gt>{'NULLABLE'} (do not forget to invert
  907. the boolean values).
  908.  
  909. =item is_not_null
  910.  
  911. Replaced with $sth-E<gt>{'NULLABLE'} (do not forget to invert
  912. the boolean values).
  913.  
  914. =item IS_NUM
  915.  
  916. Replaced with $sth-E<gt>{'TYPE'} (portable) or
  917. $sth-E<gt>{'mysql_is_num'} (MySQL specific).
  918.  
  919. =item is_num
  920.  
  921. Replaced with $sth-E<gt>{'TYPE'} (portable) or
  922. $sth-E<gt>{'mysql_is_num'} (MySQL specific).
  923.  
  924. =item IS_KEY
  925.  
  926. Replaced with $sth-E<gt>{'mysql_is_key'}.
  927.  
  928. =item is_key
  929.  
  930. Replaced with $sth-E<gt>{'mysql_is_key'}.
  931.  
  932. =item MAXLENGTH
  933.  
  934. Replaced with $sth-E<gt>{'mysql_max_length'}.
  935.  
  936. =item maxlength
  937.  
  938. Replaced with $sth-E<gt>{'mysql_max_length'}.
  939.  
  940. =item LENGTH
  941.  
  942. Replaced with $sth-E<gt>{'PRECISION'} (portable) or
  943. $sth-E<gt>{'mysql_length'} (MySQL specific)
  944.  
  945. =item length
  946.  
  947. Replaced with $sth-E<gt>{'PRECISION'} (portable) or
  948. $sth-E<gt>{'mysql_length'} (MySQL specific)
  949.  
  950. =item NUMFIELDS
  951.  
  952. Replaced with $sth-E<gt>{'NUM_OF_FIELDS'}.
  953.  
  954. =item numfields
  955.  
  956. Replaced with $sth-E<gt>{'NUM_OF_FIELDS'}.
  957.  
  958. =item NUMROWS
  959.  
  960. Replaced with the result of $sth-E<gt>execute() or
  961. $sth-E<gt>{'mysql_affected_rows'}.
  962.  
  963. =item TABLE
  964.  
  965. Replaced with $sth-E<gt>{'mysql_table'}.
  966.  
  967. =item table
  968.  
  969. Replaced with $sth-E<gt>{'mysql_table'}.
  970.  
  971. =back
  972.  
  973. =back
  974.  
  975.  
  976. =head1 MULTITHREADING
  977.  
  978. The multithreading capabilities of DBD::mysql depend completely
  979. on the underlying C libraries: The modules are working with handle data
  980. only, no global variables are accessed or (to the best of my knowledge)
  981. thread unsafe functions are called. Thus DBD::mysql is believed
  982. to be completely thread safe, if the C libraries are thread safe
  983. and you don't share handles among threads.
  984.  
  985. The obvious question is: Are the C libraries thread safe?
  986. In the case of MySQL the answer is "mostly" and, in theory, you should
  987. be able to get a "yes", if the C library is compiled for being thread
  988. safe (By default it isn't.) by passing the option -with-thread-safe-client
  989. to configure. See the section on I<How to make a threadsafe client> in
  990. the manual.
  991.  
  992.  
  993. =head1 INSTALLATION
  994.  
  995. Windows users may skip this section and pass over to L<WIN32
  996. INSTALLATION> below. Others, go on reading.
  997.  
  998. First of all, you do not need an installed MySQL server for installing
  999. DBD::mysql. However, you need at least the client
  1000. libraries and possibly the header files, if you are compiling DBD::mysql
  1001. from source. In the case of MySQL you can create a
  1002. client-only version by using the configure option --without-server.
  1003. If you are using precompiled binaries, then it may be possible to
  1004. use just selected RPM's like MySQL-client and MySQL-devel or something
  1005. similar, depending on the distribution.
  1006.  
  1007. First you need to install the DBI module. For using I<dbimon>, a
  1008. simple DBI shell it is recommended to install Data::ShowTable another
  1009. Perl module.
  1010.  
  1011. I recommend trying automatic installation via the CPAN module. Try
  1012.  
  1013.   perl -MCPAN -e shell
  1014.  
  1015. If you are using the CPAN module for the first time, it will prompt
  1016. you a lot of questions. If you finally receive the CPAN prompt, enter
  1017.  
  1018.   install Bundle::DBD::mysql
  1019.  
  1020. If this fails (which may be the case for a number of reasons, for
  1021. example because you are behind a firewall or don't have network
  1022. access), you need to do a manual installation. First of all you
  1023. need to fetch the archives from any CPAN mirror, for example
  1024.  
  1025.   ftp://ftp.funet.fi/pub/languages/perl/CPAN/modules/by-module
  1026.  
  1027. The following archives are required (version numbers may have
  1028. changed, I choose those which are current as of this writing):
  1029.  
  1030.   DBI/DBI-1.15.tar.gz
  1031.   Data/Data-ShowTable-3.3.tar.gz
  1032.   DBD/DBD-mysql-2.1001.tar.gz
  1033.  
  1034. Then enter the following commands:
  1035.  
  1036.   gzip -cd DBI-1.15.tar.gz | tar xf -
  1037.   cd DBI-1.15
  1038.   perl Makefile.PL
  1039.   make
  1040.   make test
  1041.   make install
  1042.  
  1043.   cd ..
  1044.   gzip -cd Data-ShowTable-3.3.tar.gz | tar xf -
  1045.   cd Data-ShowTable-3.3
  1046.   perl Makefile.PL
  1047.   make
  1048.   make install  # Don't try make test, the test suite is broken
  1049.  
  1050.   cd ..
  1051.   gzip -cd DBD-mysql-2.1001.tar.gz | tar xf -
  1052.   cd DBD-mysql-2.1001
  1053.   perl Makefile.PL
  1054.   make
  1055.   make test
  1056.   make install
  1057.  
  1058. During "perl Makefile.PL" you will be prompted some questions.
  1059. Other questions are the directories with header files and libraries.
  1060. For example, of your file F<mysql.h> is in F</usr/include/mysql/mysql.h>,
  1061. then enter the header directory F</usr>, likewise for
  1062. F</usr/lib/mysql/libmysqlclient.a> or F</usr/lib/libmysqlclient.so>.
  1063.  
  1064.  
  1065. =head1 WIN32 INSTALLATION
  1066.  
  1067. If you are using ActivePerl, you may use ppm to install DBD-mysql.
  1068. For Perl 5.6, upgrade to Build 623 or later, then it is sufficient
  1069. to run
  1070.  
  1071.   ppm install DBI
  1072.   ppm install DBD::mysql
  1073.  
  1074. If you need an HTTP proxy, you might need to set the environment
  1075. variable http_proxy, for example like this:
  1076.  
  1077.   set http_proxy=http://myproxy.com:8080/
  1078.  
  1079. As of this writing, DBD::mysql is missing in the ActivePerl 5.8.0
  1080. repository. However, Randy Kobes has kindly donated an own
  1081. distribution and the following might succeed:
  1082.  
  1083.   ppm install http://theoryx5.uwinnipeg.ca/ppms/DBD-mysql.ppd
  1084.  
  1085. Otherwise you definitely *need* a C compiler. And it *must* be the same
  1086. compiler that was being used for compiling Perl itself. If you don't
  1087. have a C compiler, the file README.win32 from the Perl source
  1088. distribution tells you where to obtain freely distributable C compilers
  1089. like egcs or gcc. The Perl sources are available on any CPAN mirror in
  1090. the src directory, for example
  1091.  
  1092.     ftp://ftp.funet.fi/pub/languages/perl/CPAN/src/latest.tar.gz
  1093.  
  1094. I recommend using the win32clients package for installing DBD::mysql
  1095. under Win32, available for download on www.tcx.se. The following steps
  1096. have been required for me:
  1097.  
  1098. =over
  1099.  
  1100. =item -
  1101.  
  1102. The current Perl versions (5.6, as of this writing) do have a problem
  1103. with detecting the C libraries. I recommend to apply the following
  1104. patch:
  1105.  
  1106.   *** c:\Perl\lib\ExtUtils\Liblist.pm.orig Sat Apr 15 20:03:40 2000
  1107.   --- c:\Perl\lib\ExtUtils\Liblist.pm      Sat Apr 15 20:03:45 2000
  1108.   ***************
  1109.   *** 230,235 ****
  1110.   --- 230,239 ----
  1111.       # add "$Config{installarchlib}/CORE" to default search path
  1112.       push @libpath, "$Config{installarchlib}/CORE";
  1113.  
  1114.   +     if ($VC  and  exists($ENV{LIB})  and  defined($ENV{LIB})) {
  1115.   +       push(@libpath, split(/;/, $ENV{LIB}));
  1116.   +     }
  1117.   +
  1118.       foreach (Text::ParseWords::quotewords('\s+', 0, $potential_libs)){
  1119.  
  1120.         $thislib = $_;
  1121.                                                                        
  1122. =item -
  1123.  
  1124. Extract sources into F<C:\>. This will create a directory F<C:\mysql>
  1125. with subdirectories include and lib.
  1126.  
  1127. IMPORTANT: Make sure this subdirectory is not shared by other TCX
  1128. files! In particular do *not* store the MySQL server in the same
  1129. directory. If the server is already installed in F<C:\mysql>,
  1130. choose a location like F<C:\tmp>, extract the win32clients there.
  1131. Note that you can remove this directory entirely once you have
  1132. installed DBD::mysql.
  1133.  
  1134. =item -
  1135.  
  1136. Extract the DBD::mysql sources into another directory, for
  1137. example F<C:\src\siteperl>
  1138.  
  1139. =item -
  1140.  
  1141. Open a DOS shell and change directory to F<C:\src\siteperl>.
  1142.  
  1143. =item -
  1144.  
  1145. The next step is only required if you repeat building the modules: Make
  1146. sure that you have a clean build tree by running
  1147.  
  1148.   nmake realclean
  1149.  
  1150. If you don't have VC++, replace nmake with your flavour of make. If
  1151. error messages are reported in this step, you may safely ignore them.
  1152.  
  1153. =item -
  1154.  
  1155. Run
  1156.  
  1157.   perl Makefile.PL
  1158.  
  1159. which will prompt you for some settings. The really important ones are:
  1160.  
  1161.   Which DBMS do you want to use?
  1162.  
  1163. enter a 1 here (MySQL only), and
  1164.  
  1165.   Where is your mysql installed? Please tell me the directory that
  1166.   contains the subdir include.
  1167.  
  1168. where you have to enter the win32clients directory, for example
  1169. F<C:\mysql> or F<C:\tmp\mysql>.
  1170.  
  1171. =item -
  1172.  
  1173. Continued in the usual way:
  1174.  
  1175.   nmake
  1176.   nmake install
  1177.  
  1178. =back
  1179.  
  1180. If you want to create a PPM package for the ActiveState Perl version, then
  1181. modify the above steps as follows: Run
  1182.  
  1183.   perl Makefile.PL NAME=DBD-mysql BINARY_LOCATION=DBD-mysql.tar.gz
  1184.   nmake ppd
  1185.   nmake
  1186.  
  1187. Once that is done, use tar and gzip (for example those from the CygWin32
  1188. distribution) to create an archive:
  1189.  
  1190.   mkdir x86
  1191.   tar cf x86/DBD-mysql.tar blib
  1192.   gzip x86/DBD-mysql.tar
  1193.  
  1194. Put the files x86/DBD-mysql.tar.gz and DBD-mysql.ppd onto some WWW server
  1195. and install them by typing
  1196.  
  1197.   install http://your.server.name/your/directory/DBD-mysql.ppd
  1198.  
  1199. in the PPM program.
  1200.  
  1201.  
  1202. =head1 AUTHORS
  1203.  
  1204. The current version of B<DBD::mysql> is almost completely written
  1205. by Jochen Wiedmann (I<joe@ispsoft.de>). The first version's author
  1206. was Alligator Descartes(I<descarte@symbolstone.org>), who has been
  1207. aided and abetted by Gary Shea, Andreas K÷nig and Tim Bunce
  1208. amongst others.
  1209.  
  1210. The B<Mysql> module was originally written by Andreas K÷nig
  1211. <koenig@kulturbox.de>. The current version, mainly an emulation
  1212. layer, is from Jochen Wiedmann.
  1213.  
  1214.  
  1215. =head1 COPYRIGHT
  1216.  
  1217. This module is Copyright (c) 1997-2001 Jochen Wiedmann, with code
  1218. portions Copyright (c)1994-1997 their original authors. This module is
  1219. released under the same license as Perl itself. See the Perl README
  1220. for details.
  1221.  
  1222.  
  1223. =head1 MAILING LIST SUPPORT
  1224.  
  1225. This module is maintained and supported on a mailing list,
  1226.  
  1227.     msql-mysql-modules@lists.mysql.com
  1228.  
  1229. To subscribe to this list, send a mail to
  1230.  
  1231.     msql-mysql-modules-subscribe@lists.mysql.com
  1232.  
  1233. or
  1234.  
  1235.     msql-mysql-modules-digest-subscribe@lists.mysql.com
  1236.  
  1237. Mailing list archives are available at
  1238.  
  1239.     http://www.progressive-comp.com/Lists/?l=msql-mysql-modules
  1240.  
  1241.  
  1242. Additionally you might try the dbi-user mailing list for questions about
  1243. DBI and its modules in general. Subscribe via
  1244.  
  1245.     http://www.fugue.com/dbi
  1246.  
  1247. Mailing list archives are at
  1248.  
  1249.      http://www.rosat.mpe-garching.mpg.de/mailing-lists/PerlDB-Interest/
  1250.      http://outside.organic.com/mail-archives/dbi-users/
  1251.      http://www.coe.missouri.edu/~faq/lists/dbi.html
  1252.  
  1253.  
  1254. =head1 ADDITIONAL DBI INFORMATION
  1255.  
  1256. Additional information on the DBI project can be found on the World
  1257. Wide Web at the following URL:
  1258.  
  1259.     http://www.symbolstone.org/technology/perl/DBI
  1260.  
  1261. where documentation, pointers to the mailing lists and mailing list
  1262. archives and pointers to the most current versions of the modules can
  1263. be used.
  1264.  
  1265. Information on the DBI interface itself can be gained by typing:
  1266.  
  1267.     perldoc DBI
  1268.  
  1269. right now!
  1270.  
  1271. =cut
  1272.