Sohail Shaikh Sohail Shaikh - 5 months ago 29
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

mysqli_num_rows($res)==1
...
I tried
len(rows)
or
rows.Column()
@
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 {
log.Fatal(err)
}
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

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 {
    log.Fatal(err)
} 

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