Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use DBI;
- # bind parameters to SQL statement
- my $dsn = "dbi:mysql:books:localhost";
- my $username = "root";
- my $password = "157000";
- my $dbh = DBI->connect($dsn, $username, $password)
- or die "Couldn't connect to database: $DBI::errstr";
- my $author = "Margaret Mitchell";
- # bind one parameter
- my $sth = $dbh->prepare("SELECT * FROM books.books WHERE book_author = ?");
- $sth->bind_param(1, $author);
- $sth->execute() or die "Couldn't execute sth.";
- print $sth->dump_results(), "\n";
- print "\n------------------------\n\n";
- # bind two parameters
- $sth = $dbh->prepare("SELECT * FROM books.books
- WHERE book_author = ? AND book_id = ?");
- $sth->bind_param(1, $author);
- $sth->bind_param(2, '5');
- $sth->execute() or die "Couldn't execute sth.";
- print $sth->dump_results(), "\n";
- print "\n------------------------\n\n";
- # another way to bind parameters
- $sth = $dbh->prepare("SELECT * FROM books.books
- WHERE book_author = ? AND book_id = ?");
- $sth->execute($author, '2'); # send the values to the execute() method in the same order in which they appear in the SQL statement
- print $sth->dump_results(), "\n";
- $dbh->disconnect();
Add Comment
Please, Sign In to add comment