Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

how perl can connect with mysql

by biofeng918 (Acolyte)
on Jan 09, 2005 at 11:23 UTC ( #420668=perlquestion: print w/ replies, xml ) Need Help??
biofeng918 has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl -w use strict; use DBI; #connecting the database test1 my $dbh=DBI->connect('DBI:mysql:test1') or die "cannot connect the dat +abase:".DBI->errstr; print"insert records:"; my$sth=$dbh->prepare(q{ insert into mytable(name, sex, birth,birthaddr)values(?,?,?,?) }); print"enter records:"; while($inputdata=<>){ chop $inputdata; last unless($inputdata); my($name,$sex,$birth,$birthadrr)=split(/,/,$inputdata); $sth->excute($name,$sex,$birth,$birthadrr) } # $dbh->commit&#129;G print"print the sex and the birth according to the name entered"; my $sth=$dbh->prepare('select * from mytable where name=?') or die $dbh->errstr; print"please enter the name&#129;F"; while($inputname=<>){ my @data; chomp $inputname; last unless($inputname); $sth->execute($inputname) or die "error".$sth->erstr; while(@data=$sth->fetchrow_array()){ print"sex:$data[2]t birth:$data[3]n"; } } #disconnecting $dbh->disconnect;#//

while run it ,it says that no database driver specified and DBI_DSN env var not set at line4.I just need someone to tell me why.Thank you!

while I run this overrighted version,it says access denied for user 'ODBC'@'localhost' <using paassword :NO> at line 4.,I till don't know why ,I hope someone can help me,thank you!

whILE i run these code ,it still doesn,t work,it says thatit needs explicit package name for inputdata at 13,14,15,16line and it needs explicit package name for inputname at 25,27,28,29line

I need someone to tell me why,thanks!!!

20050109 Janitored by Corion: Added formatting

Comment on how perl can connect with mysql
Download Code
Re: how perl can connect with mysql
by cchampion (Curate) on Jan 09, 2005 at 11:37 UTC

    You need at least two things:

    • a colon (:) between "mysql" and "test1". It seems there is a strange character instead (that was before you updated your code).
    • An appropriate driver (DBD::mysql), without which the DBI can't know which DBMS it should talk to.
      If you want to know if the driver is already installed, use this line:
      $ perl -MDBD::mysql -e 1
      If no error is returned, then you have the right module installed. If you get an error message, you should install it.

    HTH

      I have installed the DBD-mysql by the way of internet. So it should be the most appropiate one for this version perl.

        So now you have amended your code and made sure that the DBD module is installed.

        Did you try again running the code?

        If yes, does it work?

        If not, does it give you any messages?

        Help us to help you. Please read (and practice) Before asking a database related question ....

        Oh, BTW, (I forgot the Monastery mantra)
        use strict and warnings, and a lot of errors will fall in you net without much effort.

Re: how perl can connect with mysql
by trammell (Priest) on Jan 09, 2005 at 15:47 UTC
    The line
    $sth->excute($name,$sex,$birth,$birthadrr)
    should be
    $sth->execute($name,$sex,$birth,$birthadrr);
    for one thing.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://420668]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2014-10-20 11:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (75 votes), past polls