#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Error: Access denied for user 'root


    Hi,

    i am new with perl just trying my hand with perl for some project where i need to connect to DATABASE MYSQL.
    DBMS1.pl is working fine when i am passing VALUE through parameter.
    My program "DBMS2.pl" is throwing exception as "Error: Access denied for user 'root". in this i am accepting value from user.

    Note:- I AM USING Windows Machine.


    PROGRAM:-
    #!/usr/bin/perl
    use warnings;
    use DBI;


    print "Please Enter DataBase Name:-\n";
    my $DataBase = <STDIN>;
    print "Please Enter UserName:-\n";
    my $UserName = <STDIN>;
    print "Please Enter Password:-\n";
    my $Password = <STDIN>;
    #You're 'using' strict.pm, yet you are attempting to create global variables by writing
    #"$dbh = ... ". Just change that line to my $dbh = DBI -> connect($DSN,$user,$pass);,
    #and the same for $sth etc. That makes them local to the file and thus scoped.
    my $dbh = DBI->connect('dbi:mysql:database=$DataBase',$UserName,$Password)
    or die "Error: $DBI::errstr\n";
    print "Program Ends\n";
    print "Database Connected\n";
    my $sql1 = "show tables";
    my $sql = "select * from student";
    print "Table Connected\n";
    my $sth = $dbh->prepare($sql);
    my $sth1 = $dbh->prepare($sql1);
    $sth->execute or die "SQL Error: $DBI::errstr\n";
    $sth1->execute or die "SQL Error: $DBI::errstr\n";
    while (my @row = $sth->fetchrow_array) {
    print "@row\n";
    }
    while (my @row = $sth1->fetchrow_array) {
    print "@row\n";
    }
    PKS
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0
    Hi,

    I made changes in my program and now same is working Fine...

    I am putting my working code down.
    Description:- Connecting to MY SQL by taking user id & pass from User & showing him all databases based on his user name which he can access.

    Code
    #!/usr/bin/perl
    #use strict;
    use warnings;
    use DBI;
    print "Program Starts\n";
    print "Please Enter UserName:-";
    chomp(my $UserName = <STDIN>);
    print "Please Enter Password:-";
    chomp( $Password = <STDIN>);
    $dbh = DBI->connect('dbi:mysql:',$UserName,$Password)
    or die "Connection Error: $DBI::err\n";
    print "Database Connected\n";
    $SQLQuery = "show databases";
    $sth = $dbh->prepare($SQLQuery);
    $sth->execute or die "SQL Error: $DBI::errstr\n";
    while (@row = $sth->fetchrow_array) {
    print "@row\n";
    }
    print "Program Ends\n";
    PKS
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    776
    Rep Power
    495
    I was going to suggest you to chomp user manual input, but it seems that you found out by yourself.

  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0
    Thanks..... Try to learn new things.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    Originally Posted by Laurent_R
    I was going to suggest you to chomp user manual input, but it seems that you found out by yourself.

    This Q was also posted in the DBI topic area of perlguru, where Bill provided the answer.
    http://perlguru.com/gforum.cgi?post=...=unread#unread

IMN logo majestic logo threadwatch logo seochat tools logo