bind_col

The bind_col method is used to create a binding (an association) between a variable and a column in a SELECT statement. This binding causes the variable to be automatically updated with the current value each time.

Example:

my ($title, $author); my $sql = "SELECT title, price, author FROM library"; my $query_handle = $conn->prepare($sql); $query_handle->execute(); $query_handle->bind_col(1, \$title, undef); $query_handle->bind_col(3, \$author, undef); while($query_handle->fetch) {     print "Author: $author, Title: $title\n"; }

The preceding example prints a list of the authors and titles of all of the books in the library table. Notice that price is part of the SELECT statement but since we don’t bind it to any variable, its value will just be ignored.

The first argument in the bind_col statement is the column number of the output, with 1 being the very first column. In the preceding example, title is column 1, price column 2, and author is column 3.

The second argument is a reference to the variable we want to bind this column to.

The third argument is not used with MySQL. Some database drivers (DBDs) may use it. In that case, it is a hash of attributes.



Perl Database Programming
Perl Database Programming
ISBN: 0764549561
EAN: 2147483647
Year: 2001
Pages: 175

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net