Click to See Complete Forum and Search --> : tutorials or any examples laying around
madddidley
04-21-2005, 07:45 PM
I have been doing php for about a year and recently started doing some stuff in perl. Does any one know a straight forward way to select from a database by doing the use:mysql?
www.maddDidley.com
Nedals
04-21-2005, 08:37 PM
#!/usr/bin/perl
use strict;
use CGI;
use DBI; ## A module that links to mySQL database
## Connect to the database
my $dbh = DBI->connect('DBI:mysql:database_name','username','password',{RaiseError=>1,PrintError=>0});
my $ID = 27; # say
## ALWAYS specify the columns you want (not *). It will save you lots of headaches if you ALTER the table
my $sql = "SELECT col1, col2 FROM table WHERE id=?"; ## using a placeholder
my $sth = $dbh->prepare($sql);
$sth->execute($ID);
$sth->bind_columns(\my ($name,$address)); ## Speeds up database returns a little
while ($sth->fetch()) {
## do what you with with the $name, $address list
}
$sth->finish(); ## Optional most of the time
NOTE:
Use the variable names $dbh and $sth (you will find they are used in most tutorials)
Update:
You might want to buy 'MySQL' by Paul Dubois.
madddidley
04-22-2005, 10:41 AM
Thanks, but on my search I saw use:mysql; Is this method easier to use than the dbi or not or the same?
Nedals
04-22-2005, 10:54 AM
From CPAN reqarding the Mysql module:
As of Msql-Mysql-modules 1.19_10 M(y)sqlPerl is no longer a separate module. Instead it is emulated using the DBI drivers. You are strongly encouraged to implement new code with DBI directly. See "COMPATIBILITY NOTES" below.
madddidley
04-22-2005, 11:36 AM
oK I am going to do more research and practice thanks