Sohail Shaikh Sohail Shaikh - 1 year ago 106
SQL Question

How to get number of rows by select query using mysql in go lang

I'm new in go lang. I want to create a login verification from MySQL db.
I want a method like as in PHP

I tried
fmt.Println("No of rows are :",rows)
but it won't...
The code which i tried ... (It is a dummy code)

rows, err := db.Query("select * from userLog where u_name = ? and u_pass = ?", uname,pswd)
if err != nil {
fmt.Println("No of rows are :",rows)
defer rows.Close()

If you have another solution for login verification purpose then kindly suggest and explain it briefly Kindly help me out.

Answer Source

As i understand it you need to check if user and password exist in database. If so you can do:

var isAuthenticated bool
err := db.QueryRow("SELECT IF(COUNT(*),'true','false') FROM userLog WHERE u_name = ? AND u_pass = ?", uname, pswd).Scan(&isAuthenticated)
if err != nil {

If database contains supplied user and password isAuthenticated will be set to true.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download