CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: DBI:
Error: Access denied for user 'root

 



praveensingh
New User

Sep 18, 2013, 10:36 AM

Post #1 of 4 (18625 views)
Error: Access denied for user 'root Can't Post

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.
PKS

(This post was edited by praveensingh on Sep 18, 2013, 11:10 AM)
Attachments: DBMS2.pl (1.03 KB)
  DBMS1.pl (0.54 KB)


BillKSmith
Veteran

Sep 18, 2013, 1:41 PM

Post #2 of 4 (18618 views)
Re: [praveensingh] Error: Access denied for user 'root [In reply to] Can't Post

You must remove the newline form $UserName and $Password. Use the function chomp (ref: perldoc -f chomp).

I am not sure that this is all that is wrong, but it is needed.
Good Luck,
Bill


praveensingh
New User

Sep 19, 2013, 5:20 AM

Post #3 of 4 (18612 views)
Re: [BillKSmith] Error: Access denied for user 'root [In reply to] Can't Post

Hi,

thanks for Quick reply....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


BillKSmith
Veteran

Sep 19, 2013, 12:49 PM

Post #4 of 4 (18602 views)
Re: [praveensingh] Error: Access denied for user 'root [In reply to] Can't Post

Congratulations on fixing your scriptSmile. However, I can suggest a few improvements.

Always (Yes ALWAYS) use strict. In the extremely rare cases where you must not have it, turn it off in the smallest possible scope. It does require some effort to declare all your variables (usually with my), but it will prevent many hard-to-find errors.

There is a subtle difference between <> and <STDIN>. Use the latter only when you are certain that you need it.

You are using the default value (space) of the special variable $" in your statement

Code
print "@row\n";

If you define $" in you code, you will not be confused by those spaces the next time you have to modify your code.

There is some advantage in using a module to prompt for username and password. Not all prompt modules are available on all platforms. None of the modules have all the features you might want. Look for a module that sets a default, validates the input, allows the user to fix any errors (or optionally abort), hides the password while the user is typing, and gets rid of the newline for you.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives