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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

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


New User

Sep 18, 2013, 10:36 AM

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


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

Note:- I AM USING Windows Machine.

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


Sep 18, 2013, 1:41 PM

Post #2 of 4 (36163 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,

New User

Sep 19, 2013, 5:20 AM

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


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.

#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";



Sep 19, 2013, 12:49 PM

Post #4 of 4 (36147 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

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,


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

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