Guest User

Untitled

a guest
Mar 3rd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use DBI;
  6.  
  7. # bind parameters to SQL statement
  8.  
  9. my $dsn = "dbi:mysql:books:localhost";
  10. my $username = "root";
  11. my $password = "157000";
  12. my $dbh = DBI->connect($dsn, $username, $password)
  13. or die "Couldn't connect to database: $DBI::errstr";
  14.  
  15. my $author = "Margaret Mitchell";
  16.  
  17. # bind one parameter
  18. my $sth = $dbh->prepare("SELECT * FROM books.books WHERE book_author = ?");
  19. $sth->bind_param(1, $author);
  20. $sth->execute() or die "Couldn't execute sth.";
  21. print $sth->dump_results(), "\n";
  22. print "\n------------------------\n\n";
  23.  
  24. # bind two parameters
  25. $sth = $dbh->prepare("SELECT * FROM books.books
  26. WHERE book_author = ? AND book_id = ?");
  27. $sth->bind_param(1, $author);
  28. $sth->bind_param(2, '5');
  29. $sth->execute() or die "Couldn't execute sth.";
  30. print $sth->dump_results(), "\n";
  31. print "\n------------------------\n\n";
  32.  
  33. # another way to bind parameters
  34. $sth = $dbh->prepare("SELECT * FROM books.books
  35. WHERE book_author = ? AND book_id = ?");
  36. $sth->execute($author, '2'); # send the values to the execute() method in the same order in which they appear in the SQL statement
  37. print $sth->dump_results(), "\n";
  38.  
  39. $dbh->disconnect();
Add Comment
Please, Sign In to add comment