# Mysql error handle

**URL:** <https://www.ruby-forum.com/t/mysql-error-handle/82077>\
**Category:** Ruby\
**Created:** [January 17, 2007, 9:30am UTC](https://www.ruby-forum.com/t/mysql-error-handle/82077 "2007-01-17T09:30:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bebitzaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b782af/32.png) [@bebitzaa](https://www.ruby-forum.com/u/bebitzaa)\
**Post date:** [January 17, 2007, 9:30am UTC](https://www.ruby-forum.com/t/mysql-error-handle/82077/1 "2007-01-17T09:30:32Z")

</div>

Hello,  
I am making an connection with mysql  
Mysql.real\_connect(“127.0.0.1”,“root”,"",“f\_juso”,“3306”)

The problem is when the user and pass is correct it works very well but  
when the user and pass is wrong it display the debugger and display the  
message:  
Mysql::Error in ComenziController#login  
Access denied for user ‘roots’@‘localhost’ (using password: NO)  
and all that ruby debugger stuff.

I wan’t to get the error in ruby and to display only my message like  
“Invalid password.”  
It’s help me, because i have made an login script and i wan’t to check  
the user and password by connecting on mysql with that user and  
password.  
Thanks

---

<div class="post-metadata">

**Author:** ![bebitzaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b782af/32.png) [@bebitzaa](https://www.ruby-forum.com/u/bebitzaa)\
**Post date:** [January 17, 2007, 9:45am UTC](https://www.ruby-forum.com/t/mysql-error-handle/82077/2 "2007-01-17T09:45:27Z")

</div>

Barabule Muci wrote:

> I wan’t to get the error in ruby and to display only my message like  
> “Invalid password.”  
> It’s help me, because i have made an login script and i wan’t to check  
> the user and password by connecting on mysql with that user and  
> password.  
> Thanks

Mysql::Error is an exception, so you should just be able to rescue it  
and move on:

conn = nil  
begin  
conn = Mysql.real\_connect(“127.0.0.1”,“root”,"",“f\_juso”,“3306”)

# do something with the connection

rescue Mysql::Error =\> e  
puts “Invalid password”  
end

---

<div class="post-metadata">

**Author:** ![bebitzaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b782af/32.png) [@bebitzaa](https://www.ruby-forum.com/u/bebitzaa)\
**Post date:** [January 17, 2007, 9:47am UTC](https://www.ruby-forum.com/t/mysql-error-handle/82077/3 "2007-01-17T09:47:30Z")

</div>

I try before but i get this message:

C:/work/aplicatie/app/controllers/comenzi\_controller.rb:32: parse error,  
unexpected kRESCUE, expecting kEND  
rescue Mysql::Error =\> e  
^  
C:/work/aplicatie/app/controllers/comenzi\_controller.rb:32: parse error,  
unexpected tASSOC  
rescue Mysql::Error =\> e  
^  
C:/work/aplicatie/app/controllers/comenzi\_controller.rb:43: parse error,  
unexpected kEND, expecting $

---

<div class="post-metadata">

**Author:** ![bebitzaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b782af/32.png) [@bebitzaa](https://www.ruby-forum.com/u/bebitzaa)\
**Post date:** [January 17, 2007, 10:01am UTC](https://www.ruby-forum.com/t/mysql-error-handle/82077/4 "2007-01-17T10:01:31Z")

</div>

Barabule Muci wrote:

> I wan’t to get the error in ruby and to display only my message like  
> “Invalid password.”  
> It’s help me, because i have made an login script and i wan’t to check  
> the user and password by connecting on mysql with that user and  
> password.  
> Thanks

Couldn’t you create a separate account with a single table with only the  
user names and passwords in it? This way you can use a set login to  
access this account and check if the user account is in the table. If  
it exists then check the password. If the information is correct  
continue with logging into the desired database otherwise display the  
error message of your choice.

---

<div class="post-metadata">

**Author:** ![bebitzaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b782af/32.png) [@bebitzaa](https://www.ruby-forum.com/u/bebitzaa)\
**Post date:** [January 17, 2007, 10:10am UTC](https://www.ruby-forum.com/t/mysql-error-handle/82077/5 "2007-01-17T10:10:43Z")

</div>

No i create users on mysql with rights on tables, this is why i chose  
this metode.  
Every user has a table that can acces.

Michael W. Ryder wrote:

> Barabule Muci wrote:
> 
> > I wan’t to get the error in ruby and to display only my message like  
> > “Invalid password.”  
> > It’s help me, because i have made an login script and i wan’t to check  
> > the user and password by connecting on mysql with that user and  
> > password.  
> > Thanks
> 
> Couldn’t you create a separate account with a single table with only the  
> user names and passwords in it? This way you can use a set login to  
> access this account and check if the user account is in the table. If  
> it exists then check the password. If the information is correct  
> continue with logging into the desired database otherwise display the  
> error message of your choice.
